background

We can use @datetimeFormat (pattern = “YYYY-MM-dd “) to set the superclass or a separate field, but it’s a bit complicated to set each time. So we can use global configuration to set most cases at once, and special cases are formatted using annotations.

Meet the target

  • The requested input parameter isString(Specified format) TurnDateTo supportget,post
  • Support for Java8 date apis such as:LocalDateLocalDateTime

Define DateConvertConfig class

package com.stop.think.web.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/ * * *@author stopThink
 */
@Configuration
public class DateConvertConfig {

  @Bean
  public Converter<String, LocalDate> localDateConverter(a) {
    return new Converter<String, LocalDate>() {
      @Override
      public LocalDate convert (String source) {
        return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd")); }}; }@Bean
  public Converter<String, LocalDateTime> localDateTimeConverter(a) {
    return new Converter<String, LocalDateTime>() {
      @Override
      public LocalDateTime convert (String source) {
        return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); }}; }/** * Change Jackson's default configuration * to handle the post form, passing JSON */
  @Bean
  public ObjectMapper objectMapper(a) {
    ObjectMapper objectMapper = new ObjectMapper();
    // Do not throw an error when converting an empty object to JSON
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    // Disable throwing an exception when an unknown property is encountered
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    // Date and time formatting
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    objectMapper.registerModule(javaTimeModule);
    returnobjectMapper; }}Copy the code

Note: Regarding the custom Converter to Converter from anonymous classes to lambda expressions, most articles on the web say that when using lambda expressions, Need to add @ ConditionalOnBean on corresponding method (name = “requestMappingHandlerAdapter”) annotation, or abnormal will happen, but after my test, after increasing the annotations, date format is not correct, The SpringBoot version tested is 2.3.X or later.