The record time inserted by Mybatis is inconsistent with the time after saving to the database
problem:
The record time inserted by Mybatis is inconsistent with the time after saving to the database.
2016-12-27 12:15:24 in Java
After saving to the database, it is 2016-12-26 22:15:24
analysis:
The difference between the two times is 14 hours, which is obviously a time zone issue.
View the current time of mysql, the current time zone:
> select curtime(); # or select now() can also
+-----------+
| curtime() |
+-----------+
| 15:18: 10 |
+-----------+
> show variables like "%time_zone%";
+------------------+----- ---+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+- -----------------+--------+
2 rows in set (0.00 sec)
#time_zone indicates that mysql uses the time zone of the system, and system_time_zone indicates that the system uses the CST time zone
Solution:
Method 1: Modify dynamically through mysql command line mode
> set global time_zone ='+8:00'; ##Modify the mysql global time zone to Beijing time, which is the East 8 district where we are > set time_zone ='+8:00'; ##Modify the current session time zone > flush privileges; #effective immediately
Method 2: Modify the time zone by modifying the my.cnf configuration file
# vim /etc/my.cnf ##Add default-time_zone ='+8:00' to the [mysqld] zone # /etc/init.d/mysqld restart ##Restart mysql to make the new time zone take effect
Method 3: Set jdbcUrl, add serverTimezone=UTC parameter
jdbcUrl=jdbc\:mysql\://localhost\:3306/wos_refer?useUnicode\=true&characterEncoding\=UTF-8&serverTimezone=UTC