Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of 8 hours difference in java acquisition time

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to solve the problem of java acquisition time difference of 8 hours, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Three kinds of time error problems:

The time obtained by using new date () under java differs by 8 hours from the real local time.

There is nothing wrong with the time obtained locally, and there is a difference of 8 hours when it is stored in the database.

The database time is correct, the backend is obtained, and then returned to the front end with a difference of 8 hours.

Reason:

New date () calls jvm time, while jvm uses the default time of 0 time zone, that is, there will be a difference of 8 hours between Beijing time and Beijing time.

When mybatis transfers the local data to the mysql database server, the server will detect the data and automatically convert the date data into the corresponding time zone of the mysql server, that is, 0 time zone, so there will be a difference of 8 hours.

The return value of a method annotated with @ RestController or @ Controller+@ResponseBody in springboot defaults to Json format

Therefore, data of type date will be converted by the default Jackson framework of springboot when returned to the browser, while the default time zone of Jackson framework is GMT (8 hours less compared to China). So the final result of returning to the front end is 8 hours apart.

Solution:

Manually set the jvm time: change the time to the time in the 8th time zone:

TimeZone.setDefault (TimeZone.getTimeZone ("GMT+8"))

Warm reminder: if it is a springboot project, you can add this to the aspect, or add the following code to start the main class:

PostConstruct void started () {TimeZone.setDefault (TimeZone.getTimeZone ("GMT+8"));}

Note: do not use the following way, this method is wrong, the last time zone obtained by GMT-8 is still 0 time zone. Many tutorials on the Internet talk about the following ways, which are not good for personal testing.

TimeZone tz = TimeZone.getTimeZone ("ETC/GMT-8"); TimeZone.setDefault (tz)

Configure the database connection information in the apprication.yml file, and url adds this sentence:

& serverTimezone=GMT%2b8

It can be solved that the time stored in the database is certainly correct, while the time obtained locally may not be Beijing time. To correct the time zone of spring's json constructor, add to the application.yml file:

Or you can use annotations to add annotations to the date data of the entity entity class, so it will be Beijing time when the data data returned by the database is converted to json format, and there will be no time zone problem when it is sent back to the front end.

@ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss", timezone= "GMT+8") private Date lastTime

However, the note is: in this way, if your jvm time is still in the 0 time zone in the background, then when you need time to perform logic in the background, you should note that the time difference is still 8 hours. It is recommended to use the first method to change the entire jvm to the 8 time zone in Beijing.

The above is all the contents of this article entitled "how to solve the problem of 8 hours difference in java acquisition time". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report