当前位置:网站首页>如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
2022-06-13 01:54:00 【bug菌¹】
一、前文
作者:bug菌
公众号:猿圈奇妙屋
特别声明:原创不易,转载请附上原文出处链接和本文声明,谢谢配合。
版权声明:文章里可能部分文字或者图片来源于互联网或者百度百科,如有侵权请联系bug菌处理。
二、环境说明
环境:springboot2.3.1.REALSE + mysql5.6 + jdk1.8
三、问题描述
发现一个有意思的东西,不知道你们平时有没有注意到啊,就是你通过new Date()来获取当前系统时间,可写入数据库后时间会比当前时间少了八小时,整整8小时!那么针对该问题,我们有何办法让它精准获取北京时间呢?

很多小伙伴可能就会说了,直接将获取到该时间的基础上再加8小时不就好了,这是没问题,也可以!那么还有更简便优雅的方式么?
今天我们就来聊一聊它,看看我推荐的方案是不是比大多数小伙伴说的方式更为优雅?使用起来是不是更便捷?
那好,那么小伙伴们,接着往下看~
四、问题分析
那来思考一个问题,为何本地获取的时间没有错,而存入数据库的时候时间却少8小时?其实这个问题,就跟mysql数据库有关系了。
你使用mybatis将本地的数据传入到mysql数据库接收的时候,数据库会对数据进行检测,会把date类型的数据自动转换为mysql所对应的时区,即0时区,所以date时间会自动减去8小时。
那么针对该问题,你有何方案呢?
五、解决方案
如下呢,我提供两种解决相差8小时的解决方案,亲测有效。仅供参考,不喜勿喷。
1️⃣方案一
从数据库方面入手,既然是由于数据库机制,那么我们可以在数据库连接时指定时区参数serverTimezone,将它设置从[UTC]改为[GMT%2B8],即可。
拓展:UTC是全球标准时间 ,GMT%2B8是东八区,不能用UTC ,否则Java时间就与数据库时间会有8h时差。
具体添加如下演示:
application-test.yaml
2️⃣方案二
从获取到时间后进行解析校验,那方式就很多,你可以手动,也可以使用注解@JsonFormat,在你的entity实体类的Date字段上添加该注解,那么数据库传回的data数据就会当你转为json格式的时候就会自动转为北京时间了,这也就避免了时区差问题,前端使用也就无需再处理了,是不是很方便呐。
具体添加演示如下:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
protected Date createTime;拓展:@JsonFormat注解,有好几个属性,但我今天就给大家科普几个常用的,也就是我上方演示的这两:
- pattern: 表示日期格式。比如:yyyyMMdd、yyyy-MM-dd等。
- timezone: 默认是GMT,在中国你需要指定GMT+8 。
要是还有同学不知道alibaba开源的json处理器,真推荐用起来,封装了很多日常使用到的序列号反序列化方法类。真的超级方便好用。
直接在你的pom.xml中添加如下依赖即可:
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>如上就是我所推荐使用的两种方案,你们更倾向那种解决方案呀?或者有更好的解决方案,记得要下发评论区告知于我和我的那群小伙伴哟,独乐乐不如众乐乐。
... ...
好啦,以上就是解决时间相差8小时问题的所有解决方案内容啦,你们学废了么?如果对你有所帮助,还请不要忘记给bug菌[三连支持]哟。如果想获得更多的学习资源或者想和更多的技术爱好者一起交流,可以关注我的公众号『猿圈奇妙屋』,后台回复关键词领取学习资料、大厂面经、面试模板等海量资源,就等你来拿。
六、文末
如果你还想要学习更多,小伙伴们大可关注bug菌专门为你们创建的专栏《bug调优》,都是我一手打下的江山,持续更新中,希望能帮助到更多小伙伴们。
我是bug菌,一名想走出大山改变命运的程序猿。接下来的路还很长,都等待着我们去突破、去挑战。来吧,小伙伴们,我们一起加油!未来皆可期,fighting!
最后送大家两句话,与诸君共勉!
️做你想做的人,没有时间限制,只要愿意,什么时候都可以开始。
你能从现在开始改变,也可以一成不变;这件事,没有规矩可言,你可以活出最精彩的自己。
如果文章对您有所帮助,就请留下您的赞吧!(#^.^#);
如果喜欢bug菌分享的文章,就请给bug菌点个关注吧!(๑′ᴗ‵๑)づ╭~;
如果对文章有任何疑问,还请文末留言或者加群吧【QQ交流群:708072830】;
鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复参与讨论(请勿发表攻击言论,谢谢);
版权声明:原创不易,转载请附上原文出处链接和本文声明,版权所有,盗版必究!!!谢谢。
边栏推荐
- Logging system in chromium
- Design of distributed game server
- TensorFlow2的Conv1D, Conv2D,Conv3D机器对应的MaxPooling详解
- Using atexit to realize automatic destruct of singleton mode
- Devaxpress Chinese description --tdximageslider (picture rotation control)
- Opencv camera calibration (1): internal and external parameters, distortion coefficient calibration and 3D point to 2D image projection
- Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
- 移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
- 机器学习基础 SVM(支持向量机)
- 如何利用您的自有数据来实现营销目标?
猜你喜欢

The scientific innovation board successfully held the meeting, and the IPO of Kuangshi technology ushered in the dawn
![[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)](/img/f7/b2463d8ffe75113d352cae332046db.jpg)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)

Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
![[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer](/img/c4/0d97def5fb587b8301bcb907fc6fcf.jpg)
[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer

万字讲清 synchronized 和 ReentrantLock 实现并发中的锁

Devaxpress Chinese description --tdximageslider (picture rotation control)

Stm32 3*3 matrix key (register version)

Plumber game

URI, URL and urn difference, relation and syntax diagram

Qt实现思维导图功能(二)
随机推荐
leetcode743. Network latency (medium, Dijkstra)
URI, URL and urn difference, relation and syntax diagram
Should the audience choose observation mode or positioning mode?
How does Google's audience work?
About tkinter Canvas does not display pictures
Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
The method of drawing rounded panel with Delphi
Run Presto under docker to access redis and Bi presentation
(no plug-in) summary of vim basic shortcut keys
Get started quickly cmake
Alertwindowmanager pop up prompt window help (Part 1)
pringboot之restfull接口规范注解(二)
Shell command notes
MySQL download and installation
Restful interface specification annotation of pringboot (2)
Topic creation and running example of ROS
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO