SpringBoot中时间类型 序列化、反序列化、格式处理示例代码

 更新时间:2020年08月27日 08:37:16   作者:赵小胖0914  
这篇文章主要介绍了SpringBoot中时间类型 序列化、反序列化、格式处理示例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

【SpringBoot】 中时间类型 序列化、反序列化、格式处理

Date

yml全局配置

spring: 
 jackson:
 time-zone: GMT+8
 date-format: yyyy-MM-dd HH:mm:ss #配置POST请求Body中Date时间类型序列化格式处理,并返回

请求参数类型转换

/**
 * 时间Date转换
 * 配置GET请求,Query查询Date时间类型参数转换
 */
@Component
public class DateConverter implements Converter<String, Date> {
 @Override
 public Date convert(String source) {
 if (StringUtils.isBlank(source)) {
  return null;
 }
 if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
  return parseDate(source.trim(), "yyyy-MM-dd");
 }
 if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
  return parseDate(source.trim(), "yyyy-MM-dd HH:mm:ss");
 }
 throw new IllegalArgumentException("Invalid value '" + source + "'");
 }

 public Date parseDate(String dateStr, String format) {
 Date date = null;
 try {
  date = new SimpleDateFormat(format).parse(dateStr);
 } catch (ParseException e) {
  log.warn("转换{}为日期(pattern={})错误!", dateStr, format);
 }
 return date;
 }
}

JDK8-时间类型-LocalDateTime、LocalDate、LocalTime

/**
 * 序列化,反序列化,格式处理
 *
 * @author zc
 * @date 2020/7/9 01:42
 */
@Slf4j
@Configuration
public class JacksonCustomizerConfig {

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

 @Value("${spring.jackson.local-date-format:yyyy-MM-dd}")
 private String localDatePattern;

 @Value("${spring.jackson.local-time-format:HH:mm:ss}")
 private String localTimePattern;

 @Bean
 public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
  return builder -> {
   builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(localDateTimePattern)));
   builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(localDatePattern)));
   builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(localTimePattern)));
   builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(localDateTimePattern)));
   builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(localDatePattern)));
   builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(localTimePattern)));
  };
 }
 
 	/**
  * 时间LocalDateTime转换
  */
 @Component
 public static class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
  @Override
  public LocalDateTime convert(String source) {
   if (StringUtils.isBlank(source)) {
    return null;
   }
   if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
    return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
   }
   throw new IllegalArgumentException("Invalid value '" + source + "'");
  }
 }

 /**
  * 时间LocalDate转换
  */
 @Component
 public static class LocalDateConverter implements Converter<String, LocalDate> {
  @Override
  public LocalDate convert(String source) {
   if (StringUtils.isBlank(source)) {
    return null;
   }
   if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
    return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
   }
   throw new IllegalArgumentException("Invalid value '" + source + "'");
  }
 }
 
}

赵小胖个人博客:https://zc.happyloves.cn:4443/wordpress/

PS:springboot 时间类型配置

springboot 自带了jackson来处理时间,但不支持jdk8 LocalDate、LocalDateTime的转换。

对于Calendar、Date二种日期,转换方式有二种:

一、统一application.properties属性配置文件中加入

spring.jackson.dateFormat=yyyy-MM-dd HH:mm:ss

如果你使用了joda第三包下的时间类型,

spring.jackson.jodaDateTimeFormat=yyyy-MM-dd HH:mm:ss

此方法为全局格式,没办法处理差异化。

二、使用jackson的时间注解@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")

需要在每个日期类型上都添加,增加代码量,但更灵活性。

总结

到此这篇关于SpringBoot中时间类型 序列化、反序列化、格式处理示例代码的文章就介绍到这了,更多相关SpringBoot中时间类型 序列化、反序列化、格式处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 在deepin上如何使用Fleet开发SpringBoot 3.0.0项目

    在deepin上如何使用Fleet开发SpringBoot 3.0.0项目

    这篇文章主要介绍了在deepin上使用Fleet开发SpringBoot 3.0.0项目的过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • 通过图例了解IDEA引入JQuery实现步骤

    通过图例了解IDEA引入JQuery实现步骤

    这篇文章主要介绍了IDEA引入JQuery实现步骤图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Java源码解析之GenericDeclaration详解

    Java源码解析之GenericDeclaration详解

    这篇文章主要介绍了Java源码解析之GenericDeclaration详解。有句古话说得好,源码能使人快乐!这里分享给大家,供需要的朋友参考。
    2017-10-10
  • springboot+mybatis通过实体类自动生成数据库表的方法

    springboot+mybatis通过实体类自动生成数据库表的方法

    这篇文章主要介绍了springboot+mybatis通过实体类自动生成数据库表的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java中Reactor的反应器模式详解

    Java中Reactor的反应器模式详解

    这篇文章主要介绍了Java中Reactor的反应器模式详解,Reactor反应器模式有点儿类似事件驱动模式,当有事件触发时,事件源会将事件dispatch分发到handler处理器进行事件处理,反应器模式中的反应器角色类似于事件驱动模式中的dispatcher事件分发器角色,需要的朋友可以参考下
    2023-12-12
  • 多个版本JAVA切换的简单步骤记录

    多个版本JAVA切换的简单步骤记录

    在工作中或者学习过程中,有一些特殊情况我们需要来切换java版本来做比较,比如一些新特性等等的相关资料,这篇文章主要介绍了多个版本JAVA切换的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2024-09-09
  • Spring如何在一个事务中开启另一个事务

    Spring如何在一个事务中开启另一个事务

    这篇文章主要介绍了Spring如何在一个事务中开启另一个事务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • springboot集成mqtt的实践开发

    springboot集成mqtt的实践开发

    本篇文章主要介绍了springboot集成mqtt的实践开发,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • spring boot实现在request里解密参数返回

    spring boot实现在request里解密参数返回

    这篇文章主要介绍了Spring Boot实现在request里解密参数返回操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 解决SpringBoot jar包中的文件读取问题实现

    解决SpringBoot jar包中的文件读取问题实现

    这篇文章主要介绍了解决SpringBoot jar包中的文件读取问题实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08

最新评论