模式 "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
s:
2017-08-10 16:48:37 -0500
我之前使用 Jodatime 的 DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss Z")
成功地做到了这一点。
我正在尝试用 Java 时间 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 时间 API 中格式字符串使用的正确模式是什么?
您应该将 YYYY
部分替换为 uuuu
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss Z");
ZonedDateTime parsed = ZonedDateTime.parse("2017-08-10 16:48:37 -0500", dtf);
我正在尝试按以下格式从 Strings
解析 ZonedDateTime
s:
2017-08-10 16:48:37 -0500
我之前使用 Jodatime 的 DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss Z")
成功地做到了这一点。
我正在尝试用 Java 时间 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 时间 API 中格式字符串使用的正确模式是什么?
您应该将 YYYY
部分替换为 uuuu
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss Z");
ZonedDateTime parsed = ZonedDateTime.parse("2017-08-10 16:48:37 -0500", dtf);