Springboot web project unified time zone solution

background

During the internationalization of the springboot project, there will be inconsistencies between the time selected by the user and the time finally saved in the database. It may be that the time zone during project development and deployment was not handled properly, resulting in time conversion problems.

First understand what time zones are:

1.GMT:Greenwich Mean Time

Greenwich Mean Time; Greenwich, London, England is set as the place where 0° longitude begins. The earth is divided into a time zone every 15° longitude, and is divided into 24 time zones. The difference between adjacent time zones is one hour; for example: Beijing, China is located In East Eighth District, GMT time is 8 hours behind Beijing time.

2.UTC: Coordinated Universal Time

Coordinated Universal Time; the time obtained through rigorous calculation is accurate to the second, with an error within 0.9s. It is a more accurate world time than GMT.

3.DST: Daylight Saving Time

Summer saving time, that is, daylight saving time; it is to adjust the time one hour earlier in order to take advantage of the abundant light in summer. Many countries in North America and Europe implement daylight saving time;

4.CST: Abbreviation for four different time zones:
Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30 Australian Standard Time
China Standard Time UT+8:00 China Standard Time
Cuba Standard Time UT-4:00 Cuba Standard Time

analyze

Draw the flow process of user time data, as shown in the figure

There are several storage locations for time zones : client, server, JVM. Mysql

Process : The user's browser obtains the current time based on the client's time zone ---" Pass the time parameters to the server --" jvm selects the current time zone of the server or the time zone set by itself according to the settings --- " Store the time in mysql, mysql has its own time zone when installed

Requirements : It takes time to ensure consistency when passing in and taking out

Solution : Then you need to ensure that different time zones can be converted back and forth or that the time zones are consistent and no conversion is performed .

Resolution process

1.Client

This is the user's computer. We cannot ask the user what time zone to use, so this cannot be modified. Users may use various time zones . This is the reason for time changes and is also the fundamental factor leading to disunity .

2. Server and JVM

There are two ways on the server

(1) The JVM is on the server. If the JVM does not set a time zone, the time zone of the current server will be selected by default.

(2) Set the JVM time zone, which will block the server's time zone. The server's time zone will not affect the JVM's time zone, and will not affect the flow of user time parameters. Select this .

[Original]Java project unifies UTC time scheme

3.Mysql

Check time zone :show variables like '%time_zone%';

Set the time zone of mysql : MySQL's default time zone is UTC time zone
    
(1) Permanent modification: Modify the mysql configuration file my-default.ini, add: default-time-zone='+08:00', restart mysql to take effect, note Be sure to add it under [mysqld], otherwise unknown variable 'default-time-zone=+8:00' will appear

my-default.ini文件内:
[mysqld] 
default-time-zone='+08:00'

(2) Temporary modification: Execute the mysql command set global time_zone='+08:00', which will take effect immediately and will become invalid after restarting mysql.

set time_zone = '+8:00';
set global time_zone='+08:00';

Summary of setting the time zone : If the client does not set it, the user can change the time zone at will --" Set the JVM's time zone to UTC ---" Set the permanent time zone of mysql to UTC

code resolution process

1. The client selects the time in the local time zone

2. The front-end component determines the time to convert to UTC time zone

3. When the front end passes it to the back end, the back end uses the @DateTimeFormat annotation to convert the format, but this cannot handle time zones.

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date date;

4. The backend JVM is set to

@SpringBootApplication
public class Application {
 @PostConstruct
 void started() {
      TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
 } 
  public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
} 
}

5. The code connects to the url of the database. useLegacyDatetimeCodeThe parameter defaults to true. We need to set it manually false, otherwise it will be invalid.

spring.datasource.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC

6.mysql sets the storage time zone to UTC

7. From the database to the server, the server backend (the previous process will not change randomly, only required) uses @JsonFormat and fixes the time zone when returning to the frontend.

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

Guess you like

Origin blog.csdn.net/Mint6/article/details/114005134