Java8 LocalDate解析异常

dre*_*nda 6 java java-8

我在LocalDate中对字符串进行简单的解析:

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

不幸的是,这段代码给出了这个例外:

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
Run Code Online (Sandbox Code Playgroud)

事实很奇怪.事实上数字日期和连贯模式都有效.在我使用java.util.Date和相同的模式之前,所有工作没有问题.

你有关于这个问题的一些提示吗?

谢谢

ass*_*ias 9

LocalDate应该代表一个实际日期 - 例如,您只传递一天的名称(星期一),而这个名称无法翻译成适当的2014年5月26日.

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

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE", Locale.ITALY);
DayOfWeek day = DayOfWeek.from(fmt.parse("lun"));
Run Code Online (Sandbox Code Playgroud)