SpringBoot Date类型插入数据库始终比正确时间早一天问题解决办法

bug描述

昨天的Date插入不进去问题解决后,一直没发现其实插入的时间一直比正确的时间早一天
输出sql语句,发现insert语句还是对的,不知道为什么插入数据库之后结果就早了一天
https://blog.csdn.net/qq_37630354/article/details/82814330

尝试解决

尝试在application.property 中设置时区未果:

#time-zone start
spring.jackson.time-zone=GMT+8
#time-zone end

后来在大神一句“你们数据库是怎么连的”,我才发现application.property连接数据库的配置中已经有一句时区的设置,在第一行设置的url的末尾:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/studentsManagement?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

其实serverTimezone这个参数最初是没有的,报了下面的错:

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or
represents .....

参考博客:https://blog.csdn.net/qq_37630354/article/details/82814330 之后才加上去的。

首先我的第一印象也是改成GMT+8,但是报错:

No timezone mapping entry for 'GMT 8

后来直接删掉&serverTimezone=GMT,结果还是报了最初的时区乱码错:

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or
represents .....

又把GMT改成UTC,不报错了,但时间还是差一天。

自己重看之前那篇参考博客,以及大神指点,发现应该写成GMT%2B8,因为

“url里面,+都要用%2b”。

就好了!
bug就解决了!!

转载自:https://www.cnblogs.com/YuQiao0303/p/10054374.html

posted @ 2019-12-08 22:11  编程小大白  阅读(2346)  评论(0编辑  收藏  举报