Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.text.ParseException: Unparseable date

I get the following error: ´java.text.ParseException: Unparseable date: "Aug 31 09:53:19 2011"´ with this format: new SimpleDateFormat("MMM dd HH:mm:ss yyyy");

Does anyone see the problem?

like image 961
Stijn.V Avatar asked Aug 31 '11 08:08

Stijn.V


People also ask

How do you handle Unparseable date exception in Java?

Basically, this exception occurs due to the input string is not correspond with the pattern. You can try the below format: SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.

What is ParseException in Java?

Class ParseExceptionSignals that an error has been reached unexpectedly while parsing. See Also: Exception , Format , FieldPosition , Serialized Form.


2 Answers

Make sure you're using the correct locale. (The SimpleDateFormat(String) constructor uses the system default locale, which may not be the one you want to use.)

This works fine on my machine:

String input = "Aug 31 09:53:19 2011";
DateFormat df = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.US);
System.out.println(df.parseObject(input));

(While using Locale.FRENCH for instance, results in a ParseException.)

like image 79
aioobe Avatar answered Oct 08 '22 01:10

aioobe


The format itself is OK for the input you gave. But you might get this error if your default locale is set to something where "Aug" is not a valid abbreviation of a month name. Try using for example to Locale.US and you'll see that it will work:

DateFormat df = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.US);
Date date = df.parse("Aug 31 09:53:19 2011");
like image 34
Jesper Avatar answered Oct 07 '22 23:10

Jesper