我有一个时间戳字符串
2013-03-01T11:22:18.01Z
由 strftime("%Y:%m:%dT%H:%M:%SZ")
生成,这是 Python 语言。
现在我正在尝试使用Java的SimpleDateFormat
解析这个时间戳。我发现的很多示例代码似乎都是这样做的:
String DATE_FORMAT = "YYYY-MM-DD'T'hh:mm:ss.ssZ"
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT)
sdf.setLenient(false)
sdf.parse(timestamp) <---- throws ParseException here
但是,我收到一个错误对话框,上面写着
Unparseable date: "2013-03-01T11:22:18.01Z"
我一直在调整DATE_FORMAT
到处尝试各种各样的事情,但我已经在同一个障碍上停留了很长一段时间了。看来我需要借用 stackoverflow 社区的力量来解决这个问题。
最佳答案
除了 yyyy
与 YYYY
以及 dd
与 DD
相比,Z
> 也引起了问题。您的 SimpleDateFormat
具有如下模式:
yyyy-MM-dd'T'HH:mm:ss.ssZ
| | | | | | | | |
| | | | | | | | -----timezone (RFC 822 time zone, ex: -0800)
| | | | | | | -------seconds again (maybe you wanted SS for milliseconds)
| | | | | | ----------seconds (0-59)
| | | | | -------------minutes (0-59)
| | | | ----------------hours (0-23)
| | | ------------------the character T
| | ---------------------day in month
| ------------------------month in year
----------------------------year
但是您传递的字符串 "2013-03-01T11:22:18.01Z"
末尾只是一个 Z,而不是时区。
如果您实际上希望末尾有一个 Z
,那么您需要在格式字符串中引用它:
yyyy-MM-dd'T'HH:mm:ss.ss'Z'
就像您对T
所做的那样。来自 javadoc :
Text can be quoted using single quotes (') to avoid interpretation.
关于java - SimpleDateFormat 解析 strftime 生成的日期时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076290/