2

I am trying with two sets of date with date format :

DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");

It works fine for the Date : Fri, 26 Aug 2016 13:55:34 +0000

Not for the Date : Tue, 06 Sep 2016 11:57:14 +0100

Throws exception for the +0100 date.

 Unparseable date: "Tue, 06 Sep 2016 11:57:14 +0100" (at offset 0)
 at java.text.DateFormat.parse(DateFormat.java:555)
Prasanna Anbazhagan
  • 1,693
  • 1
  • 20
  • 37

2 Answers2

5

It fails at offset 0, which means that the problem is not related to the timezone but to the day in letters.

You should set the Locale of your SimpleDateFormat.

    DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
    Date d1 = format.parse("Fri, 26 Aug 2016 13:55:34 +0000");
    Date d2 = format.parse("Tue, 06 Sep 2016 11:57:14 +0100");

Works without any problem.

If you also need to retrieve the timezone, you will also have to add z to your pattern:

    DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
YMomb
  • 2,366
  • 1
  • 27
  • 36
3

You need

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");

Note the z for the time zone.

The parser ignores the zero (+0000) case if z is not supplied, but not a non-zero (+0100) case. The lenient property controls this behaviour (Acknowledge @Marko Topolnik).

Since you're using English week names, you ought to use the two-argument constructor to SimpleDateFormat, passing Locale.ENGLISH as the second parameter.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483