繁体   English   中英

模式“YYYY-MM-dd HH:mm:ss Z”导致解析异常,其中Jodatime没有

[英]Pattern “YYYY-MM-dd HH:mm:ss Z” causes parsing exception where Jodatime did not

我正在尝试以下列格式解析来自Strings ZonedDateTime

2017-08-10 16:48:37 -0500

我之前使用Jodatime的DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss Z")成功完成了这项工作。

我试图用Java Time API替换Jodatime,并且相同的模式不再有效。

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss Z");
ZonedDateTime.parse("2017-08-10 16:48:37 -0500", dtf);

导致以下异常:

java.time.format.DateTimeParseException: Text '2017-08-10 16:48:37 -0500' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {DayOfMonth=10, OffsetSeconds=-18000, WeekBasedYear[WeekFields[SUNDAY,1]]=2017, MonthOfYear=8},ISO resolved to 16:48:37 of type java.time.format.Parsed

在Java Time API中使用格式字符串的正确模式是什么?

你应该用uuuu替换你的YYYY部分

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss Z");
    ZonedDateTime parsed = ZonedDateTime.parse("2017-08-10 16:48:37 -0500", dtf);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM