Java SimpleDateFormat gives parse error - java

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");

Related

UnParseable Date Exception :"2015-02-06T16:05:20"

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

Unable to parse this date

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");

Converting dd-MMM-yyyy to MM/dd/yyyy format

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.

Java parse exception

I created a hibernate program and when i tried to format the date i am getting the below mentioned error:
java.text.ParseException: Unparseable date
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd");
Date date1=null;
Date date2=null;
Date startdate=(Date)formatter.parse(startDate);
Date enddate=(Date)formatter.parse(endDate);
How to resolve the parse exception?
Thanks in advance.
You are trying to parse startdate but it doesn't actually contain any date and hence the Exception.
You should try:
String strDate = "2012-10-20";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = formatter.parse(strDate);

convert String "30-MAR-07" to date object

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
dt = formatter.parse(temp[0]);
gives me the error
java.text.ParseException: Unparseable date: "30-MAR-07"
Is there any way that I can format the given String to a Date object without having to write my own string splitting and translating-to-months-in-numerals methods? Thanks
Use correct format, as follows,
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Your SimpleDateFormat pattern is completely incorrect for "30-MAR-07". It's got the values in the wrong order, it uses the wrong separator, and the wrong month format. You want:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
You may also want to specify the locale, e.g.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);
I've checked that this works - at least on my machine :)
Try this... (new SimpleDateFormat("dd-MMM-yy")
System.out.println(new SimpleDateFormat("dd-MMM-yy").format(new Date("30-MAR-07")));

Categories