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.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* LocalDateTime2JsonConfig
*
* @author yyh
*/
@Configuration
public class LocalDateTimeToJacksonConfig {
/**
* LocalDateTime格式化
*/
private static final String LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* LocalDate格式化
*/
private static final String LOCAL_DATE_FORMAT = "yyyy-MM-dd";
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
// 序列化
LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT));
LocalDateSerializer localDateSerializer = new LocalDateSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT));
// 反序列化
LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT));
LocalDateDeserializer localDateDeserializer = new LocalDateDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT));
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer)
.serializerByType(LocalDate.class, localDateSerializer)
.deserializerByType(LocalDateTime.class, localDateTimeDeserializer)
.deserializerByType(LocalDate.class, localDateDeserializer);
}
}
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;
/**
* LocalDateTimeToStringConfig
*
* @author yyh
*/
@Configuration
public class LocalDateTimeToStringConfig {
/**
* LocalDateTime格式化
*/
private static final String LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* LocalDate格式化
*/
private static final String LOCAL_DATE_FORMAT = "yyyy-MM-dd";
@Bean
public StringToLocalDateTimeConverter localDateTimeConverter() {
return new StringToLocalDateTimeConverter();
}
@Bean
public StringToLocalDateConverter localDateConverter() {
return new StringToLocalDateConverter();
}
private class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT));
}
}
private class StringToLocalDateConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT));
}
}
}