【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】 中时间类型 序列化、反序列化、格式处理的更多相关文章

  1. Spring MVC+Fastjson之时间类型序列化

    时间类型序列化: 注意红色代码,必须引入fastjson的JSONField类,而非其它. import com.alibaba.fastjson.annotation.JSONField; @Ent ...

  2. 彻底解决Spring mvc中时间类型的转换和序列化问题

    在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8时间包下的具体类型参数来直接接收.同时还有一系列的序列化 .反序列化问题,在返回前端带时间类型的同样会出 ...

  3. MySql中时间类型总结

    最近建表要用到时间类型的数据,但对时间类型的数据一向不了解,就总结了一下.. 一.日期DATE 一个日期.支持的范围是“1000-01-01”以“9999-12-31”.MySQL显示日期用 “YYY ...

  4. MVC3学习:Sql Server2005中时间类型DateTime的显示

    在Sql Server2005中,如果将某字段定义成日期时间类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如 2013/8/6 13:37:33) 如果只想显示成年月日形式,不要时 ...

  5. SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式

    在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...

  6. SpringBoot中时间格式化的5种方法!

    在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的几种方法. ​ 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项 ...

  7. java中时间类型的问题

    时间类型:System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为longimport java.sql.Date d ...

  8. SrpingMVC/SpringBoot中restful接口序列化json的时候使用Jackson将空字段,空字符串不传递给前端

    笔者的JSON如下: { "code": 10001, "message": "成功", "nextUrl": null ...

  9. mysql中时间类型datetime,timestamp与int的区别

    在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...

随机推荐

  1. Oracle调用Java方法(上)如何使用LoadJava命令和如何将简单的Jar包封装成Oracle方法

    最近在工作中遇到了遇到了一个需求需要将TIPTOP中的数据导出成XML并上传到FTP主机中,但是4GL这方面的文档比较少最终决定使用Oracle调用Java的方法,在使用的过程中发现有很多的坑,大部分 ...

  2. MySQL语句的使用

    进入数据库  mysql -u root -pmysql    (u用户名,p密码)#如果不想让其他人看到就直接一个p然后回车再打密码 select version();   查看数据库版本 sele ...

  3. 高性能IO —— Reactor(反应器)模式

    讲到高性能IO绕不开Reactor模式,它是大多数IO相关组件如Netty.Redis在使用的IO模式, 为什么需要这种模式,它是如何设计来解决高性能并发的呢? 最最原始的网络编程思路就是服务器用一个 ...

  4. 蝙蝠算法(BA)学习笔记

    算法原理 蝙蝠能够在夜间或十分昏暗的环境中自由飞翔和准确无误地捕捉食物,是因为他们能够从喉头发出地超声脉冲回声来定位.受这一启发,Yang教授在2010年提出了蝙蝠算法(Bat Algorithm,B ...

  5. Oracle数据库中,误删除或者修改数据恢复方法

    在我们实际工作中,误删除或者修改Oracle数据库中的数据,怎么办呢?这里给大家分享一种解决办法.假如你误操作的时间不超过30分钟(数据库默认的回滚保持段里的数据时间,可以在pl/sql执行窗口按ct ...

  6. JavaScript基础-即时函数(Immediate Functions)(017)

    1.即时函数的声明方法 即时函数(Immediate Functions)是一种特殊的JavaScript语法,可以使函数在定义后立即执行:(function () {    alert('watch ...

  7. Python3-在windows快速运行一个简单的本地 HTTP 服务器

    1.打开控制台2.python -m http.server

  8. 使用原生js来控制、修改CSS伪元素的方法总汇, 例如:before和:after

    在网页中,如果需要使用辅助性/装饰性的内容的时候,我们不应该直接写在HTML中,这样会影响真正的内容,这就需要使用伪元素了,这是由于css的纯粹语义化是没有意义的.在使用伪元素的时候,会发现js并不真 ...

  9. 关于display的box和flex布局

    关于二者的区别于联系,在知乎上看到有人这么回答的 flex 2012年的语法,也将是以后标准的语法,大部分浏览器已经实现了无前缀版本. box是2009年的语法,已经过时,是需要加上对应前缀的. 另外 ...

  10. P1220 关路灯——区间dp

    P1220 关路灯 题目描述 某一村庄在一条路线上安装了 \(n\) 盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一 ...