I have date string as "02-SEP-2012". I want to convert it to 8/02/2012 format.
String deliverydate="02-SEP-2012";
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
Date date=sdf.parse(deliveryDate);
This gives me
java.text.ParseException: Unparseable date: "01-Sep-2012"
How to parse this?
I want to insert this date in MS SQL.
2018/Java 10
String deliverydate="02-SEP-2012";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yyyy")
.toFormatter(Locale.UK);
LocalDate ld = LocalDate.parse(deliverydate, formatter);
System.out.println(ld);
Original answer
Look at the two Strings, 02-SEP-2012 and MM/dd/yyyy - these are not the same format.
The date parser is expecting the String to be in the same format as the formatter
String deliverydate="02-SEP-2012";
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy");
Date date=sdf.parse(deliveryDate);
sdf=new SimpleDateFormat("MM/dd/yyyy");
System.out.println(sdf.format(date));
You should use:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse(deliveryDate);
then to re-format:
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));
in sql server:
You could use convert() function..
You can try
Insert into <table> (date_col,<othercolumns>)
select CONVERT(date,'02-SEP-2012'),<othercolumns>
Example:
declare #date varchar(20) ='02-SEP-2012'
select CONVERT(date,#date)
result:
2012-09-02
Change the SimpleDateFormat
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy");
String deliverydate="02-SEP-2012";
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
Date date=sdf.parse(deliveryDate);
Did you try this "- " instead of "/".
Using the parse(String source) method can throw java.text.ParseException exception if the supplied date is not in a valid format.So you should use valid format.
Related
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 6 years ago.
I have date in string object. I want to convert into Date object.
Date getDateFmString(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(dateString);
return convertedCurrentDate ;
}
above function returning following output.
Fri Apr 22 00:00:00 IST 2016
but I want output in this format '2016-03-01' only
function should take string only.
function should return Date object.
I have done lot of research over web, but I got solution from one Expert.
Date getDateFrmString(String dDate)
{
java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
return dDate;
}
this is what I want.
Change the date format from
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
to
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Hope this works
See this example
public Class DateFormatDemo{
public static void main (String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";
try{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch(ParseException e){
e.printStackTrace();
}
}
}
This link might help you with string to date object conversions
You are parsing with the wrong format try
String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Output:
2016-01-01
DEMO1
And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.
String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
OUTPUT:
01-01-2016
DEMO2
To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.
Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".
Let's do it step by step:
You have a date as String in dd-MM-yyyy format.
You want to convert it into date. (for this you are using SimpleDateFormat)
Now you are printing the date. Question here is are you printing the converted date object or input string?
If its a date object then toString method is called of date class.
As per comment on java.util.Date class it's:
dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016
So that coincides with what you get in output in the second approach. But how is that code even running is strange.
String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);
Variable 'input' is not defined.
What are the possible solutions:
While printing date, convert it back to String using SimpleDateFormat as per the requirement.
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dStr = sdf.format(dateString);
System.out.printn(dStr);
Extending class java.util.Date and override toString, but that would be a bad idea.
I have a date object which has date in the format: 2015-09-21 10:42:48:000
I want it to be displayed on the UI in this format.21-Sep-2015 10:42:48
The code I am using is not working and throws this:
Unparseable date exception: Unparseable date: "2015-09-21 10:42:48"
Here is the actual code:
String createdOn=f.getCreatedOn().toString();//f.getCreatedOn() returns a date object
SimpleDateFormat format=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date=format.parse(createdOn.substring(0,createdOn.length()-3));
log.debug(">>>>>>date now is: "+date);
model.addAttribute("date", date);
model.addAttribute("info", messages);
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
format1.format(date);
log.debug(">>>>>>date now is again: "+date);
Unparseable date exception: Unparseable date: "2015-09-21 10:42:48"
Since your input date format is yyyy-MM-dd HH:mm:ss. But you are trying parsing with dd-MMM-yyyy HH:mm:ss format.
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//change input date format here
Date date=format.parse("2015-09-21 10:42:48:000");
//Date date=format.parse(createdOn);//Here no need of subtracting 000 from your date
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
System.out.println(format1.format(date));
SimpleDateFormat doc
Your input date format is different.
String createdOn="2015-09-21 10:42:48:000";
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=format.parse(createdOn.substring(0,createdOn.length()-4));
System.out.println(">>>>>>date now is: "+date);
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
format1.format(date);
System.out.println(">>>>>>date now is again: "+date);
You are using wrong format to display and parse.
// We will use this for parsing a string that represents a date with the format declared below. If you try to parse a date string with a different format, you will get an exception like you did
SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// This is for the way we want it to be displayed
SimpleDateFormat displayFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
// Parse the date string
Date date = parseFormat.parse("2015-09-21 10:42:48:000");
// Format the date with the display format
String displayDate = displayFormat.format(date);
System.out.println(">>>>>>date now is: " + displayDate);
I have a Date Variable "StartTime", in which i need to store this input String "2015-02-06T16:05:20"
I tried like below, but it gives Unparsable Date Exception. What i am doing wrong?
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String inputTime = "2015-02-06T16:05:20";
setStartTime(dateFormat.parse(inputTime));
You can change you date format to "yyyy-MM-dd'T'HH:mm:ss"
Read more Java SimpleDateFormat
I am getting dates in this format 14-MAY-13 01.17.16.250000 PM and
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss aaa");
simpleDateFormat.parse("14-MAY-13 01.17.16.250000 PM")
Gives me this exception
java.text.ParseException: Unparseable date: "14-MAY-13 01.17.16.250000 PM" ..What should be format of simpledateformat?
Try
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa");
The format must be corrected (see the javadoc for more information). You should also set the Locale to the SimpleDateFormat to be sure the month will be parsed with the correct language.
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa", Locale.US);
If you don't give the Locale, SimpleDateFormat will use the default locale of your system which can be not the locale of the given string input.
You need to use the SSSSSS for milliseconds and replace : with . to match this part 01.17.16.250000.
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa");
In your Formatstring you expect hh:mm:ss but in your input string your have hh.mm.ss and you also didn't include the miliseconds.
So the format string should be dd-MMM-yy hh.mm.ss.SSSSSS aaa
Simple date format and date formate should be same, try this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss:SSSSSS aaa");
Date d = simpleDateFormat.parse("14-MAY-13 01:17:16:250000 PM");
I am doing the following
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
calendar.setTime(formatter.parse("01/26/2012");
When I do
calendar.getTime()
I see
java.text.ParseException: Unparseable date: "01/26/2012"
What is am I doing wrong?
Thank you
Erm, you give the pattern
MM-dd-yyyy
to SimpleDateFormat then offer it
01/26/2012
to parse. That's not the format you specified, and it's telling you it can't parse it.
You would need:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
You've set the format to
MM-dd-yyyy
And the you expect it to parse the string on a format: MM/dd/yyyy
Try
new SimpleDateFormat("MM/dd/yyyy");