2

I'm trying to use the SimpleDateFormat class to parse a DateTime out of this string:

Mon Jan 10 2011 01:15:00 GMT+0000 (GMT Standard Time)

I tried the following format string:

String example = "Mon Jan 10 2011 01:15:00 GMT+0000 (GMT Standard Time)";
SimpleDateFormat formatter = new SimpleDateFormat("E M d y H:m:s z");
try
{
    Date exampleDate = formatter.parse(example);
    LOGGER.warn(exampleDate.toString());
}
catch(Exception e)
{
    LOGGER.warn(e.getMessage(), e);
}

But it generates the error:

Unparseable date: "Mon Jan 10 2011 01:15:00 GMT+0000 (GMT Standard Time)"

So I tried removing the parenthesized end part of the example string:

String example = "Sun Jan 09 2011 22:00:00 GMT+0000";

But it generates the same error.

WARNING: Unparseable date: "Sun Jan 09 2011 22:00:00 GMT+0000"
java.text.ParseException: Unparseable date: "Sun Jan 09 2011 22:00:00 GMT+0000"

Any hints on how to get around this?

4

4 回答 4

2

According to http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html the format needs to be:

"E M d y H:m:s zZ"

This includes the general timezone and the RFC 822 time zone.

Or you could change your input date to:

Mon Jan 10 2011 01:15:00 GMT+00:00

which would accomodate just z.

(Note the colon in the last part.)

于 2011-01-12T01:06:29.520 回答
2

您应该使用(或Z仅用于最后一部分):

E MMM dd yyyy HH:mm:ss zZ 
于 2012-09-18T19:13:52.577 回答
1

如果要解析文本月份,还需要使用“MMM”。从javadocs:

“月份:如果模式字母的数量为3个或更多,则将月份解释为文本;否则,将其解释为数字。”

于 2011-01-12T01:23:49.163 回答
0

I think that the problem is that the z modifier can't parse GMT+0000. According to the Javadoc describing what z parses, the format is something like GMT+HH:MM, rather than GMT+HHMM. If you want to parse what you have, you probably want to change your format string from

E M d y H:m:s z

to

E M d y H:m:s 'G'M'Tz
于 2011-01-12T01:10:42.263 回答