Reference: blog.csdn.net/A1962747112… Springboot version: 2.2.1.release

Today, I used @datetimeFormat to mark in the field of the object, but I found that the parameters in the front end could not be successfully converted to the date. After a long search, I found that it was caused by a small problem.

Note: LocalDateTime cannot use pattern = “YYYY-MM-DD”

Otherwise error:

Field error in object 'userParam' on field 'endTime': rejected value [2020-06-18]; codes [typeMismatch.userParam.endTime,typeMismatch.endTime,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userParam.endTime,endTime]; arguments []; default message [endTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2020-06-18'; nested exception is java.time.format.DateTimeParseException: Text '2020-06-18' could not be parsed at index 10]
Copy the code

Incorrect usage:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDateTime endTime;

@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDateTime endTime;
Copy the code

Correct usage:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate endTime;

@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate endTime;
Copy the code