Springboot全局时间格式化配置

我们对字段的日期格式化时一般会用注解:

 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
 private Date createDate;

但是每个字段都要写也太麻烦了 不是我的全局化作风 在application.yml中配置全局时间格式化只会对Date类型有用:

  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    timezone: GMT+8
    serialization:
      write-dates-as-timestamps: false
  mvc:
    format:
      date: yyyy-MM-dd
      time: HH:mm:ss
      date-time: yyyy-MM-dd HH:mm:ss

以上是无效的 java8的时间模块比如LocalDateTime就没用了 时间依然中间有个T 所以需要额外配置 自定义一个配置类顺便把反序列化也配了:

package com.hmdp.config;

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.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
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.LocalTime;
import java.time.format.DateTimeFormatter;


@Configuration
public class JacksonConfigure {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer(@Value("${spring.mvc.format.date-time}") String dateTimeFormatPattern,
                                                            @Value("${spring.mvc.format.date}") String dateFormatPattern,
                                                            @Value("${spring.mvc.format.time}") String timeFormatPattern
    ) {
        return jacksonObjectMapperBuilder -> {
            JavaTimeModule javaTimeModule = new JavaTimeModule();

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern);
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(dateFormatPattern);
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(timeFormatPattern);

            javaTimeModule.addSerializer(new LocalDateTimeSerializer(dateTimeFormatter));
            javaTimeModule.addSerializer(new LocalDateSerializer(dateFormatter));
            javaTimeModule.addSerializer(new LocalTimeSerializer(timeFormatter));

            javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
            javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
            javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));

            jacksonObjectMapperBuilder.modules(javaTimeModule);
        };
    }
}

加上这些配置后还要检查一下项目有没有jackson-datatype-jsr310-2.13.5.jar这个依赖 这是jackson对java8时间模块的支持包 没有pom.xml就要加上:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.13.5</version> 
</dependency>

大功告成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值