我正在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"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句