温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • SpringBoot中时间类型 序列化、反序列化、格式处理示例代码

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

发布时间:2020-10-15 06:38:33 来源:脚本之家 阅读:193 作者:赵小胖0914 栏目:开发技术

【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/

总结

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI