Misbehaving of the date parsing in SimpleDateFormat

I have a java class inside the jar file that is in a JBoss server which invoked through a bash file as follows.

java -cp /com/site/domain/TimeFormatter.jar packOne.subPack.Test

But I got an error when parsing the below date in there.

java.text.ParseException: Unparseable date: "Wed, 29 Jan 2014 21:00:00 GMT"
    at java.text.DateFormat.parse(DateFormat.java:335)

Java CODE :

Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z").parse("Sun, 26 Jan 2014 21:00:00 GMT");
System.out.println("main().date : " + date);

What am I missing here? I read many posts about the misbehaving of the SimpleDateFormat. But this is some thing that I didn't find among those posts.

Note : I thought this is because of the java version change or some thing. But, I executed the same program with different versions in my machine. It worked perfectly. Server java version "1.5.0_22"

locale details :



Works fine for me, and I think it's not a Java version-related issue... but more of a Locale problem :)

Specifying Locale.ENGLISH for your SimpleDateFormat should definitively make it work. For example, you could do:

final Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH).parse("Sun, 26 Jan 2014 21:00:00 GMT");

As specified in the javadoc entry for SimpleDateFormat, the date parsing is locale-sensitive. I have to admit that I don't exactly know how your string does not match your Locale, but we can investigate further if... we know what your default Locale actually is :)

The modern Java date and time classes had not quite come out when this question was first asked and answered. I should like to contribute the modern solution that I recommend using nowadays.

    ZonedDateTime dateTime = ZonedDateTime.parse("Wed, 29 Jan 2014 21:00:00 GMT", 

I was about to write the same about the need for a locale as in the other answer, but then I saw the light: your date-time string conforms to the RFC 1123 format. This is built-in with Java-8 and later and therefore also with ThreeTen Backport, the backport of the modern classes to Java 6 and 7. DateTimeFormatter.RFC_1123_DATE_TIME works in English always, independent of locale. So the above is all you need. Tested on a computer with Danish default locale.

Need Your Help

WebHttpBinding Security Question

wcf ssl wcf-security webhttpbinding

I have created a RESTful Service and implemented the Authentication. It accepts username and password and then grants access to the service requested. It Works fine. Now I want to use SSL on top of...

Can Indy run Javascript?

javascript delphi indy

There is a software product called AnyChart which is great for embedding Flashed based charts in web pages. AnyCharts can also export to PNG file format. Here is an example: