查看当前服务器下的时间:
[root@swk-204 ~]# dateFri Jan 25 19:28:28 CST 2019[root@swk-204 ~]# date "+%Y-%m-%d %H:%M:%S"2019-01-25 19:28:36[root@swk-204 ~]#
问题解决设置操作系统的时区:
[root@engine ~]# tzselectPlease 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 Ocean10) Pacific Ocean11) none - I want to specify the time zone using the Posix TZ format.#? 5Please select a country. 1) Afghanistan18) Israel35) Palestine 2) Armenia19) Japan36) Philippines 3) Azerbaijan20) Jordan37) Qatar 4) Bahrain21) Kazakhstan38) Russia 5) Bangladesh22) Korea (North)39) Saudi Arabia 6) Bhutan23) Korea (South)40) Singapore 7) Brunei24) Kuwait41) Sri Lanka 8) Cambodia25) Kyrgyzstan42) Syria 9) China26) Laos43) Taiwan10) Cyprus27) Lebanon44) Tajikistan11) East Timor28) macau45) Thailand12) Georgia29) Malaysia46) Turkmenistan13) Hong Kong30) Mongolia47) United Arab Emirates14) India31) Myanmar (Burma)48) Uzbekistan15) Indonesia32) Nepal49) Vietnam16) Iran33) Oman50) Yemen17) Iraq34) Pakistan#? 9Please select one of the following time zone regions.1) east China - Beijing, Guangdong, Shanghai, etc.2) Heilongjiang (except Mohe), Jilin3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.4) most of Tibet & Xinjiang5) west Tibet & Xinjiang#? 1The following information has been given:Chinaeast China - Beijing, Guangdong, Shanghai, etc.Therefore TZ='Asia/Shanghai' will be used.Local time is now:Fri Jan 25 15:10:18 CST 2019.Universal Time is now:Fri Jan 25 07:10:18 UTC 2019.Is the above information OK?1) Yes2) No#? 1You can make this change permanent for yourself by Appending the lineTZ='Asia/Shanghai'; export TZto the file '.profile' in your home directory; then log out and log in again.Here is that TZ value again, this time on standard output so that youcan use the /usr/bin/tzselect command in shell scripts:Asia/Shanghai[root@engine ~]#
调整时区文件到对应目录备份当前的时区配置
[root@engine ~]# mv /etc/localtime /etc/localtime-old
替换系统时区文件
[root@engine ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
或者创建链接文件
[root@engine ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改clock系统配置文件/etc/sysconfig/clock为如下内容
[root@swk-204 ~]# cat /etc/sysconfig/clockZONE="Asia/Shanghai"UTC=false#设置为false,硬件时钟不于utc时间一致ARC=false[root@swk-204 ~]#
设置操作系统环境变量TZ
在/etc/profile或~/.bashrc文件中设置环境变量TZ
export TZ='Asia/Shanghai'
或者
【国外服务器部署springboot 项目 出现时区问题】TZ='Asia/Shanghai'; export TZ
通过source命令即可完成设置
JAVA进程调整时区
AVA硬编码
import java.util.TimeZone;import javax.annotation.PostConstruct;import org.springframework.stereotype.Component;@Componentpublic class TimeZoneStartup {/*** 设置时区*/@PostConstructpublic void init(){TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));}}
按照以上方法操作后,可以获取指定的时区
推荐阅读
- 直播云服务器对带宽有什么要求?带宽承载直播用户计量方式
- 如何购买域名和服务器?服务器和域名大概需要多少钱?
- 美国服务器设置禁PING的方法步骤
- 如何将不同linux服务器的目录内容进行双向同步
- 高性能的Web服务器Gunicorn 20.1配置Superset 1.4
- nginx实现TCP转发
- 网管跑路,防火墙重置就算了,还让我们重做域服务器,而且不加域
- Docker部署微服务
- 服务器安全篇:最可怕的搜索引擎 运维必备之——Shodan
- 74块钱一年的云服务器可以用来做什么?