LocalDateTime报错“Unable to obtain LocalDateTime from TemporalAccessor…”

An exception is thrown when converting a string in yyyy-MM-dd format to a LocalDateTime object.

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

solution

Converts a string in the format yyyy-MM-dd first to a LocalDate and then to a LocalDateTime.

LocalDateTime date = LocalDate.parse("2023-06-02").atStartOfDay();

Supongo que te gusta

Origin blog.csdn.net/watson2017/article/details/131397014
Recomendado
Clasificación