一、为鸡毛研究它呢
最近上线国外的项目,存在时间不一致问题,我们要用的是美洲时间(America/New_York),所以在百度上面一顿google,常言说,百度一下,你就知道,但是百度一个缺点就是,知道的太多了,尼玛,这么多,就没**好用的,一个个试水吧,闲话少叙,奔正题;
二、考虑不一致的因素
- 服务器因素: centos 7
- 服务自身因素: spring boot 项目
- 数据库因素: mysql
三、一个一个来
1、服务器尝试:
[root@localhost ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 2
Please select a country.
1) Anguilla 19) Dominican Republic 37) Peru
2) Antigua & Barbuda 20) Ecuador 38) Puerto Rico
3) Argentina 21) El Salvador 39) St Barthelemy
4) Aruba 22) French Guiana 40) St Kitts & Nevis
5) Bahamas 23) Greenland 41) St Lucia
6) Barbados 24) Grenada 42) St Maarten (Dutch)
7) Belize 25) Guadeloupe 43) St Martin (French)
8) Bolivia 26) Guatemala 44) St Pierre & Miquelon
9) Brazil 27) Guyana 45) St Vincent
10) Canada 28) Haiti 46) Suriname
11) Caribbean NL 29) Honduras 47) Trinidad & Tobago
12) Cayman Islands 30) Jamaica 48) Turks & Caicos Is
13) Chile 31) Martinique 49) United States
14) Colombia 32) Mexico 50) Uruguay
15) Costa Rica 33) Montserrat 51) Venezuela
16) Cuba 34) Nicaragua 52) Virgin Islands (UK)
17) Curaçao 35) Panama 53) Virgin Islands (US)
18) Dominica 36) Paraguay
#? 49
Please select one of the following time zone regions.
1) Eastern (most areas) 16) Central - ND (Morton rural)
2) Eastern - MI (most areas) 17) Central - ND (Mercer)
3) Eastern - KY (Louisville area) 18) Mountain (most areas)
4) Eastern - KY (Wayne) 19) Mountain - ID (south); OR (east)
5) Eastern - IN (most areas) 20) MST - Arizona (except Navajo)
6) Eastern - IN (Da, Du, K, Mn) 21) Pacific
7) Eastern - IN (Pulaski) 22) Alaska (most areas)
8) Eastern - IN (Crawford) 23) Alaska - Juneau area
9) Eastern - IN (Pike) 24) Alaska - Sitka area
10) Eastern - IN (Switzerland) 25) Alaska - Annette Island
11) Central (most areas) 26) Alaska - Yakutat
12) Central - IN (Perry) 27) Alaska (west)
13) Central - IN (Starke) 28) Aleutian Islands
14) Central - MI (Wisconsin border) 29) Hawaii
15) Central - ND (Oliver)
#? 1
The following information has been given:
United States
Eastern (most areas)
Therefore TZ='America/New_York' will be used.
Local time is now: Thu Sep 3 08:31:18 EDT 2020.
Universal Time is now: Thu Sep 3 12:31:18 UTC 2020.
Is the above information OK?
1) Yes
2) No
#? 1
[root@localhost etc]# date
2020年 09月 03日 星期四 20:44:23 CST
注释:虽然都选了,就是不好使,就是尼玛不好使(可能是需要重启)
划重点,来个好使的: 搞了好几个小时,尼玛,一句话搞定;
cp /usr/share/zoneinfo/America/New_York /etc/localtime
[root@localhost etc]# date
2020年 09月 03日 星期四 20:44:23 CST
[root@localhost etc]# cp /usr/share/zoneinfo/America/New_York /etc/localtime
cp:是否覆盖"/etc/localtime"? y
[root@localhost etc]# date
2020年 09月 03日 星期四 08:45:08 EDT
来吧,启动项目测试吧,还是有问题:还是有问题,这尼玛接着弄吧:
2、考虑时间创建的位置,项目中date,数据库中默认;那就从这两方面考虑
一顿百度··············································································································
有结果了:
在启动类里面增加:
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
@PostConstruct
void setDefaultTimezone() {
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
}
在配置文件里面增加:
spring :
jackson:
time-zone: America/New_York
注释:这个还行,好使
3、继续数据库时区修改
### 第一种
select NOW();
##一:通过sql命令临时修改
set global time_zone = '-4:00';
set time_zone = '-4:00';
flush privileges;
select NOW();
### 第二种:修改my.cnf实现永久修改
>>>>>>>> mysql代码 <<<<<<<
二:修改my.cnf实现永久修改
vi /etc/mysql/my.cnf
然后在mysqld下边的配置(Basic Settings)中添加一行:
default-time_zone = '-4:00'
然后重启mysql
service mysql restart
>mysql: select now(); ##查看当前时间
注释:建议直接第二种,当然了,我这里是纽约时间,请根据自己实际情况修改