SpringBoot返回date日期格式化

SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差

 

 

问题描述

 

       在Spring Boot项目中,使用@RestController注解,返回的java对象中若含有date类型的属性,则默认输出为TIMESTAMP时间戳格式(数据库datetime类型查询出来就是时间戳) ,如下所示:

 

 

解决方案
       解决方案有多种,这里只记录本人觉得最简单的一种。一般我们的日期显示格式为:yyyy-MM-dd HH:mm:ss,所以我们可以在配置文件中进行全局配置。

 

在application.properties配置文件增加以下配置::

 

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
第一行指定date输出格式为yyyy-MM-dd HH:mm:ss;

 

第二行指定时区,解决8小时的时间差问题。

 

运行结果:

 

 

 

 

参照上面的方式如果还还是解决不了,可以在实体类上加上 下面这个注解

 

 


 

参考:https://blog.csdn.net/beauxie/article/details/78552919 

 

 

 

 

 

 

springboot2.0后,spring会将时间自动给转成UTC字符串了
springboot1.x版本的将date字段返回的是时间戳
配置返回时间戳

spring
jackson:
serialization:
write-dates-as-timestamps: true

 

全局配置返回字符串

spring:
    jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8
posted @ 2019-07-01 19:14  panchanggui  阅读(12105)  评论(0编辑  收藏  举报