java.text.ParseException: Unparseable date “yyyy-M

2019-01-23 01:29发布

I would appreciate any help with finding bug for this exception:

java.text.ParseException: Unparseable date: "2007-09-25T15:40:51.0000000Z"

and following code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = sdf.parse(timeValue);
long mills = date.getTime();
this.point.time = String.valueOf(mills);

It throws expcetion with Date date = sdf.parse(timeValue); .

timeValue = "2007-09-25T15:40:51.0000000Z"; , as in exception.

Thanks.

2条回答
乱世女痞
2楼-- · 2019-01-23 02:06

In Java 7 you can also use the X pattern to match an ISO8601 timezone, which includes the special Z (UTC) value:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX");
Date date = sdf.parse("2007-09-25T15:40:51.0000000Z");

However, it seems to require an exact number of millisecond characters in the pattern, which is not required for the 'Z' character pattern, and is rather inconvenient. I think this is because the ISO8601 definition also includes "two-digit hours", which are just numbers, so cannot be distinguished by the parser from the preceding milliseconds.

So this version would be fine for timestamps down to second precision, less so for milliseconds.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-23 02:08

Z represents the timezone character. It needs to be quoted:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
查看更多
登录 后发表回答