1. 使用Jackson
导入依赖包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
2. 使用方法
2.1 单个使用
@JsonFormat(pattern = " yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
2.2 配置文件使用
2.2.1 yml
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
2.2.2 properties
spring.jackson.date-format =yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
2.3 JAVA代码使用
@Configuration
public class LocalDateTimeSerializerConfig {
@Value("${spring.jackson.date-format}")
private String pattern;
@Bean
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
}