Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String “2020-01-15”: Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text ‘2020-01-15’ could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2020, MonthOfYear=1, DayOfMonth=15},ISO of type java.time.format.Parsed; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String “2020-01-15”: Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text ‘2020-01-15’ could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2020, MonthOfYear=1, DayOfMonth=15},ISO of type java.time.format.Parsed at [Source: (PushbackInputStream); line: 2, column: 18] (through reference chain: com.example.demo.Demo[“localDate”])]
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "2020-01-15": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2020-01-15' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2020, MonthOfYear=1, DayOfMonth=15},ISO of type java.time.format.Parsed; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2020-01-15": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2020-01-15' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2020, MonthOfYear=1, DayOfMonth=15},ISO of type java.time.format.Parsed
at [Source: (PushbackInputStream); line: 2, column: 18] (through reference chain: com.example.demo.Demo["localDate"])]
SpringBoot 2.4.2 后:
u才是代表年的那个字母,而y是指带有纪元(era)的年
在DateTimeFormatter严格模式下使用,yyyy-MM-dd并不合法,正确的使用姿势是uuuu-MM-dd
带上G表示一下公元前或者公元后AD/BC
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd G")
.withResolverStyle(ResolverStyle.STRICT);
LocalDate localDate = LocalDate.parse("2021-01-20 AD", formatter);