我正在尝试使用 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
。
(注意最后一部分的冒号。)
关于java - 为什么此代码会生成错误 : "unparseable date",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4664420/