Java DateTimeFormatterBuilder with optional pattern results in DateTimeParseException

前端 未结 4 1394
傲寒
傲寒 2021-01-19 13:30

Goal

Provide a flexible parser for LocalDate instances that can handle input in one of the following formats:

  • yyyy
  • yyyyMM
  • yyyyMMdd<
4条回答
  •  耶瑟儿~
    2021-01-19 14:21

    You set a value for month and day but pass a month and year. That's the problem.

    You may want to use :

    .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
    .parseDefaulting(ChronoField.YEAR_OF_ERA, ZonedDateTime.now().getYear())
    

提交回复
热议问题