Spring获取日期时间差8小时问题解决

这个问题,恶心的一批。。无力吐槽

原文地址:https://blog.csdn.net/m0_38043362/article/details/78855196?utm_source=blogxgwz8

导致这个问题的原因,网上搜了一下,大概意思是:spring转json的默认实现jackson中会根据时区去转换时间,而jackson的默认时区跟国内是相差8小时的,所以这里得重新设置当前项目地所在时区。

1. application.yml 或者application.properties 文件中数据库连接方式追加时区设置serverTimezone=GMT+8,这里转换+号为 %2b 后使用

spring:
    datasource:
        # 主数据源
        datasource:
            driverClassName: com.mysql.jdbc.Driver
            jdbc-url: jdbc:mysql://localhost:3306/ga_qhfj_microffice?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8&useSSL=false
            username: root
            password: root

2.  application.properties 或  application.yml添加配置

#application.properties文件配置
spring.jackson.time-zone=GMT+8
 
------------------------------------
 
#application.yml文件配置
spring:
    jackson:
        time-zone: GMT+8

3.返回的实体类使用注解@JsonFormat

public class ArticleListVo {
    private Long articleId;
 
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;
}

 

2019.6.10

这些方法都有些问题,本来我用的是第二种,结果发现有问题的时间戳正常了项目中其他地方的个别的时间戳又错了,还是用最笨的办法吧

在实体中多加两个String属性用来接收日期

在sql语句中将出问题的时间转成字符串,在起个加的字段名称的别名,在页面中展示着两个新加的属性

sql中Data 转 字符串 方法如下

DATE_FORMAT(table.start_time,'%Y-%m-%d %h:%i:%s') AS startTimenew

DATE_FORMAT(table.end_time,'%Y-%m-%d %h:%i:%s') AS endTimenew,

问题解决。。

posted @ 2019-05-31 19:52  狴颜丶领衔  阅读(1992)  评论(0编辑  收藏  举报