MySQL时区问题

January 16, 2019

相差14或8个小时

解决方案:在mysql连接字符串后加上 serverTimezone=GMT%2b8 ,表示使用 GMT+8北京时间

1
jdbc:mysql://ip:3306/seckiluseUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8

或者,进入mysql,执行 show variables like ‘%time_zome%’ 查看当前时区

1
2
3
4
5
6
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+

修改time_zone

set global time_zone=’+08:00’;

set time_zone = ‘+08:00’;

再次查看:

1
2
3
4
5
6
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | +08:00 |
+------------------+--------+




@ResponseBody返回对象时时间字段时结果会相差14或8个小时

解决方案:spring boot使用Jackson进行对象的序列化,需要我们手动配置Jackson的时区

1
2
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

时区概念:

  • GMT:Greenwich Mean Time 格林威治时间
  • CST: China Standard Time 中国标准时间
  • UTC: 世界标准时间

GMT+8=UTC+8=CST