[Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`

1. When data of type localdate in Java is tested on swagger, the format when input in the format of json is 2018-07-09. It should be noted that 07 and 09 are two digits, not one digit.

2. If the date is of LocalDate type, JacksonAutoConfiguration will automatically process whether the foreground transmits the date in string format to the background or the background returns the date in format to the front end.

3. If the date is of LocalDateTime type, we need to process it from the front end to the back end and from the back end to the front end. Because the configuration in the following YML does not apply to Java 8 date types, such as LocalDate and LocalDateTime, it only applies to fields of date or DateTime type.

#Date Formatting
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss

Solution: add LocalDateTimeConfig configuration class

/**
 * LocalDateTime
 */
@Configuration
public class LocalDateTimeGlobalConfig {
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     *Configuring LocalDateTime type serialization and deserialization
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        /*return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
                jacksonObjectMapperBuilder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
            }
        };*/
        //This approach is equivalent to the above
        return builder -> {
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
        };
    }
}

Read More: