Java8:LocalDate: parse yyyy-MM

LocalDate: parse yyyy-MM

LocalDate转换年月,会有java.time.format.DateTimeParseException
解决方法:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM");
YearMonth ym = YearMonth.parse("2020-04", fmt);
LocalDate dt = ym.atEndOfMonth();
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    .appendPattern("yyyy-MM")
    .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
    .toFormatter();
LocalDate dt = LocalDate.parse("2020-04", fmt);

你可能感兴趣的:(Java8)