简体   繁体   English

Java8 LocalDate解析异常

[英]Java8 LocalDate parse Exception

I'm doing a simple parse of a string in LocalDate: 我在LocalDate中对字符串进行简单的解析:

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

Unfortunally, this code give this exception: 不幸的是,这段代码给出了这个例外:

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

The fact is quite strange. 事实很奇怪。 Infact with numeric date and coherent pattern all works. 事实上数字日期和连贯模式都有效。 Before I used java.util.Date and the same pattern and all worked without problems. 在我使用java.util.Date和相同的模式之前,所有工作没有问题。

Have you some hints about this problem? 你有关于这个问题的一些提示吗?

Thanks 谢谢

LocalDate is supposed to represent an actual date - you only pass a day name (Monday) which can't be translated into a proper 26-May-2014, for example. LocalDate应该代表一个实际日期 - 例如,您只传递一天的名称(星期一),而这个名称无法翻译成适当的2014年5月26日。

If all you want is to parse the day of week, you can use: 如果你想要的只是解析星期几,你可以使用:

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

Using 运用

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

works! 作品!

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

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