Springboot 全局时间格式化操作

作者:陈晨辰~ 时间:2022-08-17 03:23:51 

时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格式化处理,通常会用到 SimpleDateFormat 工具处理。


SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date stationTime = dateFormat.parse(dateFormat.format(PayEndTime()));

可一旦处理的地方较多,不仅 CV 操作频繁,还产生很多重复臃肿的代码,而此时如果能将时间格式统一配置,就可以省下更多时间专注于业务开发了。

可能很多人觉得统一格式化时间很简单啊,像下边这样配置一下就行了,但事实上这种方式只对 date 类型生效。


spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

而很多项目中用到的时间和日期API 比较混乱, java.util.Date 、 java.util.Calendar 和 java.time LocalDateTime 都存在,所以全局时间格式化必须要同时兼容性新旧 API。

看看配置全局时间格式化前,接口返回时间字段的格式。


@Data
public class OrderDTO {
   private LocalDateTime createTime;
   private Date updateTime;
}

很明显不符合页面上的显示要求(有人抬杠为啥不让前端解析时间,我只能说睡服代码比说服人容易得多~)

一、@JsonFormat 注解

@JsonFormat 注解方式严格意义上不能叫全局时间格式化,应该叫部分格式化,因为@JsonFormat 注解需要用在实体类的时间字段上,而只有使用相应的实体类,对应的字段才能进行格式化。


@Data
public class OrderDTO {
   @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
   private LocalDateTime createTime;
   @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
   private Date updateTime;
}

字段加上 @JsonFormat 注解后,LocalDateTime 和 Date 时间格式化成功。

二、@JsonComponent 注解(推荐)

这是我个人比较推荐的一种方式,前边看到使用 @JsonFormat 注解并不能完全做到全局时间格式化,所以接下来我们使用 @JsonComponent 注解自定义一个全局格式化类,分别对 Date 和 LocalDate 类型做格式化处理。


@JsonComponent
public class DateFormatConfig {

@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
   private String pattern;

/**
    * @author xiaofu
    * @description date 类型全局时间格式化
    * @date 2020/8/31 18:22
    */
   @Bean
   public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() {

return builder -> {
           TimeZone tz = TimeZone.getTimeZone("UTC");
           DateFormat df = new SimpleDateFormat(pattern);
           df.setTimeZone(tz);
           builder.failOnEmptyBeans(false)
                   .failOnUnknownProperties(false)
                   .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                   .dateFormat(df);
       };
   }

/**
    * @author xiaofu
    * @description LocalDate 类型全局时间格式化
    * @date 2020/8/31 18:22
    */
   @Bean
   public LocalDateTimeSerializer localDateTimeDeserializer() {
       return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
   }

@Bean
   public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
       return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
   }
}

看到 Date 和 LocalDate 两种时间类型格式化成功,此种方式有效。

@JsonComponent 注解处理格式化

但还有个问题,实际开发中如果我有个字段不想用全局格式化设置的时间样式,想自定义格式怎么办?

那就需要和 @JsonFormat 注解配合使用了。


@Data
public class OrderDTO {
   @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
   private LocalDateTime createTime;

@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
   private Date updateTime;
}

从结果上我们看到 @JsonFormat 注解的优先级比较高,会以 @JsonFormat 注解的时间格式为主。

三、@Configuration 注解

这种全局配置的实现方式与上边的效果是一样的。

注意:在使用此种配置后,字段手动配置@JsonFormat 注解将不再生效。


@Configuration
public class DateFormatConfig2 {

@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
   private String pattern;
   public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   @Bean
   @Primary
   public ObjectMapper serializingObjectMapper() {
       ObjectMapper objectMapper = new ObjectMapper();
       JavaTimeModule javaTimeModule = new JavaTimeModule();
       javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
       javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
       objectMapper.registerModule(javaTimeModule);
       return objectMapper;
   }

/**
    * @author xiaofu
    * @description Date 时间类型装换
    * @date 2020/9/1 17:25
    */
   @Component
   public class DateSerializer extends JsonSerializer<Date> {
       @Override
       public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {
           String formattedDate = dateFormat.format(date);
           gen.writeString(formattedDate);
       }
   }

/**
    * @author xiaofu
    * @description Date 时间类型装换
    * @date 2020/9/1 17:25
    */
   @Component
   public class DateDeserializer extends JsonDeserializer<Date> {

@Override
       public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
           try {
               return dateFormat.parse(jsonParser.getValueAsString());
           } catch (ParseException e) {
               throw new RuntimeException("Could not parse date", e);
           }
       }
   }

/**
    * @author xiaofu
    * @description LocalDate 时间类型装换
    * @date 2020/9/1 17:25
    */
   public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
       @Override
       public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
           gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
       }
   }

/**
    * @author xiaofu
    * @description LocalDate 时间类型装换
    * @date 2020/9/1 17:25
    */
   public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
       @Override
       public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
           return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));
       }
   }
}

SpringBoot全局日期格式转换失效问题记录

今天新搭建了一个项目, 像以前一样在一个配置类上做了个全局字符串转换日期对象的转换器!


   @Bean
   public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
       return builder -> {
          .............
       };
   }
}
//-----------------
//或者使用官网提供的 通过注解`@JsonComponent`来声明其静态内部类,都没生效!

但是发现失效了, 在debug模式下根本没有进来,没有任何反应! 后面发现原因在继承了WebMvcConfigurationSupport配置类,导致自动配置失效!

在网上找到的原因, 如下图所示:

Springboot 全局时间格式化操作

自动配置在当WebMvcConfigurationSupport类不存在的时候才会生效WebMvc自动化配置,WebMvc自动配置类中不仅定义了classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/等路径的映射,还定义了配置文件spring.mvc开头的配置信息等。

类路径上的 HttpMessageConverter 失效

解决方案

是在自己继承了WebMvcConfigurationSupport上配置转换器:


@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport  {

/**
    * 解决继承WebMvcConfigurationSupport,静态资源访问不到
    *
    * @param registry
    * @author: ZhiHao
    * @date: 2020/6/11
    */
   @Override
   protected void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
       super.addResourceHandlers(registry);
   }
   /**
    * 解决继承WebMvcConfigurationSupport, json方式全局日期反序列化失效
    *
    * @param converters
    * @author: ZhiHao
    * @date: 2020/6/11
    */
   @Override
   protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
       MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
       ObjectMapper objectMapper = new ObjectMapper();
       SimpleModule module = new SimpleModule();
       module.addDeserializer(Date.class, new JsonDeserializer<Date>() {
           @Override
           public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
               return DateConverterConfig.parse(jsonParser.getText());
           }
       });
       objectMapper.registerModule(module);
       converter.setObjectMapper(objectMapper);
       converters.add(converter);
       super.configureMessageConverters(converters);
   }
   /**
    * 解决继承WebMvcConfigurationSupport, 普通请求,String转换Date-转换器
    *
    * @param registry
    * @author: ZhiHao
    * @date: 2020/6/11
    */
   @Override
   protected void addFormatters(FormatterRegistry registry) {
       registry.addConverter(new DateConverterConfig());
   }
}

来源:https://chenchenchen.blog.csdn.net/article/details/108433289

标签:Springboot,全局,时间,格式化
0
投稿

猜你喜欢

  • Android自定义实现图片加文字功能

    2022-08-03 22:05:49
  • Java实现插入公式到PPT的示例代码

    2023-11-12 03:04:41
  • Java swing读取txt文件实现学生考试系统

    2021-06-13 17:41:02
  • Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】

    2023-11-25 12:14:40
  • C语言 奇偶排序算法详解及实例代码

    2023-04-17 04:47:39
  • Collections.shuffle()方法实例解析

    2021-09-17 18:51:38
  • 小菜编程成长记(一 面试受挫——代码无错就是好?)第1/3页

    2023-06-08 19:15:26
  • 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路

    2021-09-04 05:48:21
  • C#实现图片切割、切图、裁剪

    2022-10-24 15:12:19
  • Android如何实现APP自动更新

    2021-10-31 06:09:27
  • Android Studio自动提取控件Style样式教程

    2022-01-29 14:09:55
  • C# Soap调用WebService的实例

    2021-06-18 20:27:08
  • java参数传递之值传递和引用传递

    2021-09-26 10:28:46
  • Android集成百度地图开发流程和注意事项

    2022-03-21 01:56:01
  • Kotlin启动协程的三种方式示例详解

    2023-06-07 02:58:53
  • Android在Fragment中实现监听触摸事件

    2023-08-22 05:24:34
  • Java使用pulsar-flink-connector读取pulsar catalog元数据代码剖析

    2023-11-05 17:25:41
  • 详解java封装实现Excel建表读写操作

    2023-12-24 11:17:20
  • Java 深入探讨设计模式之原型模式篇

    2023-11-16 17:37:59
  • Mybatis通过数据库表自动生成实体类和xml映射文件

    2022-01-11 07:05:46
  • asp之家 软件编程 m.aspxhome.com