首页 > 其他分享 >SpringMVC中LocalDate、LocalDateTime、LocalTime、Date的序列化与反序列化

SpringMVC中LocalDate、LocalDateTime、LocalTime、Date的序列化与反序列化

时间:2022-11-22 19:05:57浏览次数:54  
标签:return Converter SpringMVC public DateTimeFormatter LocalDateTime new 序列化 class

这是在使用SpringMVC时经常会遇到的日期类型转换,直接上代码:

@Configuration
public class ConverterConfig {

private Logger logger = LoggerFactory.getLogger(ConverterConfig.class);

private String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";

private String localDateFormat = "yyyy-MM-dd";

private String localTimeFormat = "HH:mm:ss";

private TimeZone timeZone = TimeZone.getTimeZone("GMT+8");

private Locale locale = Locale.CHINA;

@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
logger.info("日期与字符串互转:Locale:【{}】, TimeZone:【{}】, Date:【{}】, LocalDateTime:【{}】,LocalDate:【{}】,LocalTime:【{}】",
locale, timeZone.getDisplayName(), dateTimeFormat, dateTimeFormat, localDateFormat, localTimeFormat);


final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormat);
final DateTimeFormatter localDateFormatter = DateTimeFormatter.ofPattern(localDateFormat);
final DateTimeFormatter localTimeFormatter = DateTimeFormatter.ofPattern(localTimeFormat);
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);

return builder -> {
//地区、时区
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.locale(locale).timeZone(timeZone);
// 序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter))
.serializerByType(LocalDate.class, new LocalDateSerializer(localDateFormatter))
.serializerByType(LocalTime.class, new LocalTimeSerializer(localTimeFormatter))
.serializerByType(Date.class, new DateSerializer(false, simpleDateFormat));
// 反序列化
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(localDateFormatter))
.deserializerByType(LocalTime.class, new LocalTimeDeserializer(localTimeFormatter))
.deserializerByType(Date.class,
new DateDeserializers.DateDeserializer(DateDeserializers.DateDeserializer.instance,
simpleDateFormat, dateTimeFormat));
};
}

/**
* LocalDate转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern(localDateFormat));
}
};
}

/**
* LocalDateTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(dateTimeFormat));
}
};
}

/**
* LocalTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
return LocalTime.parse(source, DateTimeFormatter.ofPattern(localTimeFormat));
}
};
}

/**
* Date转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);

try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
logger.error("Init Converter String to Date error!", e);
throw new RuntimeException(e);
}
}
};
}

}


欢迎关注我的公众号“平凡的程序员”。

SpringMVC中LocalDate、LocalDateTime、LocalTime、Date的序列化与反序列化_LocalDateTime

标签:return,Converter,SpringMVC,public,DateTimeFormatter,LocalDateTime,new,序列化,class
From: https://blog.51cto.com/qiguang/5878346

相关文章

  • 19:使用pickle实现序列化和反序列化_神经元记忆移植
    ###使用pickle序列化Python中,一切皆对象,对象本质上就是一个“存储数据的内存块”。有时候,我们需要将“内存块的数据”保存到硬盘上,或者通过网络传输到其他的计算机上。......
  • SpringMVC
    SpringMVCDispatcherServlet<servlet>    <servlet-name>springMVC</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherSe......
  • SpringMVC知识
    1Spring框架Spring框架指的都是SpringFramework,它是很多模块的集合,使用这些模块可以很方便地协助我们进行开发。Spring自带IoC(InverseofControl:控制反转)和A......
  • springmvc 项目启动后自动运行方法
    packagecom.jeeplus.modules.asr.config;importcom.jeeplus.common.config.Global;importcom.jeeplus.modules.asr.netty.server.UDPServer;importcom.jeeplus.modu......
  • SpringMVC - RestFul
    一、RestFul是一个请求路径的风格,将请求参数放在url中以/来分割请求参数。url:/delete/1    1就是参数RestFul的请求方式对应的是不同的操作get : 查询post:......
  • Java序列化与反序列化
    序列化保证对象可传递性和完整性将对象转为字节流,可以保存在本地或在网上传输保存对象状态和重建反序列化根据字节流,重建对象为什么需要序列化与反序列化分布式对象......
  • 木马免杀代码篇之python反序列化分离免杀(一)
    前言本篇文章主要用到python来对CobaltStrike生成的Shellcode进行分离免杀处理,因此要求读者要有一定的python基础,下面我会介绍pyhon反序列化免杀所需用到的相关函数和......
  • LocalDate,LocalTime,LocalDateTime之间的转化和常用应用
    Java8中,对于日期、时间、时间日期有不同的对象来表示,分别就是LocalDate、LocalTime、LocalDateTime他们都位于java.time包下,并且他们都仅单纯的表示一个不可变的时间对象,......
  • [C# 中的序列化与反序列化](.NET 源码学习)
    [C#中的序列化与反序列化](.NET源码学习)关键词:序列化(概念与分析)   三种序列化(底层原理源码)   Stream(底层原理源码)   反射(底层原理源码)假如有一天我们要......
  • SpringMVC - 获取请求参数,作用域
    一、获取请求参数1.原生serveltAPI@ControllerpublicclassTestController{@RequestMapping("/test01")publicStringtest01(HttpServletRequestrequest){......