1

I have a timestamp string 2013-03-01T11:22:18.01Z generated by strftime("%Y:%m:%dT%H:%M:%SZ"), which is in Python.

Now I'm trying to use Java's SimpleDateFormat to parse this timestamp. Lot's of sample code that I found seem to do this:

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

However, I get an error dialog that says Unparseable date: "2013-03-01T11:22:18.01Z"

I've been tweaking DATE_FORMAT here and there, trying all sorts of things but I've been stuck on the same roadblock for quite a while now. Looks like I'll need to borrow the power of the stackoverflow community on this.

4

2 回答 2

1

除了yyyyvsYYYYddvs之外DDZ还有一个问题。你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,而不是时区。

如果您实际上期望 aZ在末尾,那么您需要在格式字符串中引用它:

yyyy-MM-dd'T'HH:mm:ss.ss'Z'

就像你为T. 从javadoc

可以使用单引号 (') 引用文本以避免解释。

于 2013-06-12T22:32:17.550 回答
1

1) 2013-03-01T11:22:18.01 的正确模式是"yyyy-MM-dd'T'HH:mm:ss.SSS"(如果 .01 是毫秒)

2) 2013-03-01T11:22:18.01Z 中的 Z 时区无法由仅接受 -0800 格式 (RFC 822) 的 SimpleDateFormat 'Z' 解析。即使您将其更改为“z”(通用时区),它仍然不会解析 Z,它应该是 GMT

请参阅 SimpleDateFormat API。

3) 2013-03-01T11:22:18.01Z 看起来非常像 XSD dateTime 格式http://www.schemacentral.com/sc/xsd/t-xsd_dateTime.html并且可以使用 java.xml 中的 DatatypeConverter.parseDateTime 进行解析.bind 包(Java 标准库)

于 2013-06-12T22:17:52.227 回答