把yyyy-MM-dd 格式的字符串转为 LocalDateTime 对象时抛出异常。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd");
LocalDateTime date = LocalDateTime.parse("2023-06-02", formatter);
java.time.format.DateTimeParseException: Text '2023-06-02' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2023, DayOfMonth=2, MonthOfYear=6},ISO of type java.time.format.Parsed
解决方案
将yyyy-MM-dd 格式的字符串先转换为LocalDate,然后再转换为LocalDateTime。
LocalDateTime date = LocalDate.parse("2023-06-02").atStartOfDay();