栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

Date

yml全局配置

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

请求参数类型转换


@Component
public class DateConverter implements Converter {
 @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


@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)));
  };
 }
 
 	
 @Component
 public static class LocalDateTimeConverter implements Converter {
  @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 + "'");
  }
 }

 
 @Component
 public static class LocalDateConverter implements Converter {
  @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中时间类型 序列化、反序列化、格式处理内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号