簡體   English   中英

Java8 LocalDate解析異常

[英]Java8 LocalDate parse Exception

我在LocalDate中對字符串進行簡單的解析:

log.debug("----->" + DateTimeFormatter.ofPattern("EEE").format(LocalDateTime.now()));
    log.debug("--->" +   LocalDate.parse("lun",DateTimeFormatter.ofPattern("EEE",Locale.ITALY)));

不幸的是,這段代碼給出了這個例外:

java.time.format.DateTimeParseException: Text 'lun' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=1},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1854)
at java.time.LocalDate.parse(LocalDate.java:400)
at it.Main.main(Main.java:60)
 ... 11 more
     Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=1},ISO of type java.time.format.Parsed
at java.time.LocalDate.from(LocalDate.java:368)
at java.time.LocalDate$$Lambda$15/42768293.queryFrom(Unknown Source)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
... 13 more
      Exception running application it.Main

事實很奇怪。 事實上數字日期和連貫模式都有效。 在我使用java.util.Date和相同的模式之前,所有工作沒有問題。

你有關於這個問題的一些提示嗎?

謝謝

LocalDate應該代表一個實際日期 - 例如,您只傳遞一天的名稱(星期一),而這個名稱無法翻譯成適當的2014年5月26日。

如果你想要的只是解析星期幾,你可以使用:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE", Locale.ITALY);
DayOfWeek day = DayOfWeek.from(fmt.parse("lun"));

運用

DateTimeFormatter.ofPattern("EEE").parse("lun").get(ChronoField.DAY_OF_WEEK);

作品!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM