簡體   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