我试图使用SimpleDateFormat类从此字符串中解析出DateTime:
Mon Jan 10 2011 01:15:00 GMT+0000 (GMT Standard Time)
我尝试了以下格式字符串:
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);
}
但这会产生错误:
Unparseable date: "Mon Jan 10 2011 01:15:00 GMT+0000 (GMT Standard Time)"
因此,我尝试删除示例字符串的括号结尾部分:
String example = "Sun Jan 09 2011 22:00:00 GMT+0000";
但是它会产生相同的错误。
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"
关于如何解决这个问题的任何提示?
最佳答案
根据http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html的格式需要为:
"E M d y H:m:s zZ"
这包括常规时区和RFC 822时区。
或者,您可以将输入日期更改为:
Mon Jan 10 2011 01:15:00 GMT+00:00
只能容纳
z
。(请注意最后一部分中的冒号。)