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) 
1
  • ParseException I guess Commented Mar 14, 2012 at 14:24

5 Answers 5

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...).

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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

Comments

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

1 Comment

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.
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

2 Comments

I'd already tried that, I should make that a bit clearer in the question.
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?
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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.