Can't parse String to LocalDate (Java 8)

限于喜欢 提交于 2019-11-27 13:56:14

For year you have to use the lowercase y:

final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy");

Uppercase Y is used for weekyear. See the javadoc of DateTimeFormatter for more details.

The answer to the question is to use 'y' not 'Y'.

To explain the error message, lets decompose it:

Unable to obtain LocalDate from TemporalAccessor

This is saying that it cannot create a LocalDate (what was requested) from a TemporalAccessor (the low-level interface that provides hashmap-like access to the fields of date/time).

of type java.time.format.Parsed

This is saying that the object passed into the method was of type java.time.format.Parsed. This is the standard output type of parsing, and contains all the information that was parsed.

{DayOfMonth=1, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2015},ISO

This is the toString() form of the java.time.format.Parsed object that resulted from parsing. It is saying that four things were parsed:

  • DayOfMonth=1, the day-of-month parsed with value of 1
  • MonthOfYear=7, the month-of-year parsed with value of 7
  • WeekBasedYear[WeekFields[MONDAY,4]]=2015, the week-based-year parsed with value of 2015
  • ISO, which is the ISO calendar system (a default value)

Since it is not possible to produce a LocalDate from the combination DayOfMonth + MonthOfYear + WeekBasedYear, an exception is thrown.

Note that the [WeekFields[MONDAY,4]] part refers to the fact that there are many different ways to define a week, in the US weeks start on Sunday, but in the ISO standard and the EU they start on Monday.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!