3

I'm receiving a ParseException with the following code and I can't seem to fix it:

String date = "Tue Mar 13 2012 10:48:05 GMT-0400";
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzzX"); //Tried zzzZ at the end as well
System.out.println(format.parse(date));

If I take out the -0400 and the X (or Z) at the end of the SimpleDateFormat things work fine, but once it's in the code, it simply doesn't work. What symbol should I be using instead? I'm using Java 7.

Here is the parse error I receive:

java.text.ParseException: Unparseable date: "Tue Mar 13 2012 10:48:05 GMT-0400"
at java.text.DateFormat.parse(DateFormat.java:357)
java.text.ParseException: Unparseable date: "Tue Mar 13 2012 10:48:05 GMT-0400"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.throwaway.parse.DateParsing.testDate(TestDate:17)
Scott
  • 9,458
  • 7
  • 54
  • 81

5 Answers5

2

The GMT part of GMT-0400 of your string is causing the problem.
The Z (or X in java 7) parameter is only matching -4000. You have to escape GMT by using single quotes like this :

DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US);

Note that it's also a good practice to put a Local in your DateFormat. Without it your code won't work in other countries (like here in France...).

YCI
  • 359
  • 1
  • 4
  • 11
  • This works, but I'm not so sure I can always count on the time zone returning GMT, someone might switch a server setting and I could get EST or PST or whatever the case might be. – Scott Mar 14 '12 at 14:56
  • I think youre timezone format "GMT-0400" is equal to the common "-0400" where "GMT" is implicit.When describing a timezone using hours and minutes it's always diffrence with GMT as reference. Something like "EST-0400" would be a very strange way to express a timezone. Also if you controle the way to string is generated you can change the timezone format to make it less ambiguous – YCI Mar 14 '12 at 15:03
2

Three issues all dealing with mixed usage. Either:

  1. Use a single lower-case "z" and a ":" separating your hour and time in the time zone when using "GMT(+/-)hh:mm", or
  2. Use a single upper-case "Z" and drop the "GMT" from your timezone, and you can use the "(+/-)hhmm" format, or
  3. Use a single upper-case "X" and still drop the "GMT" but you can use any format of the hhmm zone.

From the Javadoc:

  • z General time zone Pacific Standard Time; PST; GMT-08:00
  • Z RFC 822 time zone -0800
  • X ISO 8601 time zone -08; -0800; -08:00
Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45
0

The pattern zzzz could only parse "GMT-04:00" style strings. Your example can be parsed with this pattern: EEE MMM dd yyyy HH:mm:ss Z

jabal
  • 11,987
  • 12
  • 51
  • 99
  • Unfortunately that does not work either. If I take out the -0400, and remove the X or the Z at the end of my original format the date parses just fine. – Scott Mar 14 '12 at 14:27
0

use "EEE MMM dd yyyy HH:mm:ss zzzZ".
zzz is for GMT and Z is for 'RFC 822 time zone' please refer

Check this out

shams.haq
  • 703
  • 1
  • 6
  • 20
  • I'd already tried that, I should make that a bit clearer in the question. – Scott Mar 14 '12 at 14:34
  • if u can put a white-space between GMT and -0400 u will be able to parse the date, whats the source of ur input string? – shams.haq Mar 14 '12 at 14:59
0

If you always expect your timezone to be represented that way, you could put "GMT" in single quotes in your format string to prevent it from being parsed:

EEE MMM dd yyyy HH:mm:ss 'GMT'XX

It's a bit weird that none of the built-in formats can parse it though. Perhaps the Javadoc is incorrect when it lists GMT-08:00 as an example of z?

matts
  • 6,738
  • 1
  • 33
  • 50