当前位置:首页 > CN2资讯 > 正文内容

linux 时区 java linux的时区

2天前CN2资讯

1:使用date命令查看时区

[root@db-server ~]# date -RSun, 11 Jan 2015 07:10:28 -0800[root@db-server ~]#

如上RFC 2822 format所示,上面命令输出了-0800表示西八区,是美国旧金山所在的时区,下面表示我们国家的东八区(+0800)

[root@lnx01 ~]# date -RSun, 11 Jan 2015 23:06:02 +0800

 

2:查看clock系统配置文件

[root@db-server ~]# more /etc/sysconfig/clockZONE="America/Los_Angeles"UTC=trueARC=false 我的香港服务器,默认时区是 Asia/Urumqi (乌鲁木齐) 使用 date 查看日期是0600,比北京时间慢两个小时左右

 

如上所示,表示该系统设置的时区为“America/Los_Angeles”,也就是西八区。

inux系统如何设置系统所在的时区呢?

方法1:使用tzselect设置时区

例子,将系统时区设置为东八区(北京时间)

[root@db-server ~]# tzselectPlease identify a location so that time zone rules can be set correctly.Please select a continent or ocean.1) Africa2) Americas3) Antarctica4) Arctic Ocean5) Asia6) Atlantic Ocean7) Australia8) Europe9) Indian Ocean10) Pacific Ocean11) none - I want to specify the time zone using the Posix TZ format.#? 5Please select a country.1) Afghanistan 18) Israel 35) Palestine2) Armenia 19) Japan 36) Philippines3) Azerbaijan 20) Jordan 37) Qatar4) Bahrain 21) Kazakhstan 38) Russia5) Bangladesh 22) Korea (North) 39) Saudi Arabia6) Bhutan 23) Korea (South) 40) Singapore7) Brunei 24) Kuwait 41) Sri Lanka8) Cambodia 25) Kyrgyzstan 42) Syria9) China 26) Laos 43) Taiwan10) Cyprus 27) Lebanon 44) Tajikistan11) East Timor 28) Macau 45) Thailand12) Georgia 29) Malaysia 46) Turkmenistan13) Hong Kong 30) Mongolia 47) United Arab Emirates14) India 31) Myanmar (Burma) 48) Uzbekistan15) Indonesia 32) Nepal 49) Vietnam16) Iran 33) Oman 50) Yemen17) Iraq 34) 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: Sun Jan 11 23:31:51 CST 2015.Universal Time is now: Sun Jan 11 15:31:51 UTC 2015.Is the above information OK?1) Yes2) No#? yesPlease enter 1 for Yes, or 2 for 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

注意:tzselect命令只告诉你选择的时区的写法,并不会生效。所以现在它还不是东8区北京时间。你可以在.profile、.bash_profile或者/etc/profile中设置正确的TZ环境变量并导出。 例如在.bash_profile里面设置 TZ='Asia/Shanghai'; export TZ并使其生效。

[root@db-server ~]# source .bash_profile[root@db-server ~]# dateSun Jan 11 23:37:40 CST 2015

 

方法2:复制相应的时区文件,替换系统时区文件;或者创建链接文件

 

 

在/usr/share/zoneinfo/下面有很多时区文件,如下所示,可以复制这些时区文件覆盖/etc/localtime文件,或修改符号链接/etc/locatime对应的文件

北京时间 [root@db-server ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@db-server ~]# date -RMon, 12 Jan 2015 10:42:26 +0800 美国时间[root@db-server ~]# cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtimecp: overwrite `/etc/localtime'? y[root@db-server ~]# date -RSun, 11 Jan 2015 18:42:49 -0800[root@db-server ~]#

注意如果有时候,执行了上面命令后,使用date -R发现时区设置没有生效,有可能是因为你在profile或.bash_profile里面设置了TZ,如下案例所示:

[root@db-server ~]# date -RSun, 11 Jan 2015 18:42:49 -0800[root@db-server ~]# vi .bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/binTZ='Asia/Shanghai'; export TZexport PATHunset USERNAME~~".bash_profile" 14L, 221C written[root@db-server ~]# source .bash_profile[root@db-server ~]# date -RMon, 12 Jan 2015 10:44:35 +0800

 

方式2:修改链接/etc/locatime的时区文件

 

[root@db-server ~]# ln /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ln: creating hard link `/etc/localtime' to `/usr/share/zoneinfo/Asia/Shanghai': File exists

[root@db-server ~]# rm /etc/localtime

rm: remove regular file `/etc/localtime'? y

[root@db-server ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

[root@db-server ~]# date -R

Mon, 12 Jan 2015 10:56:10 +0800

[root@db-server ~]#

 

方法3:使用timeconfig设置时区


inux系统如何设置系统所在的时区呢?

方法1:使用tzselect设置时区

例子,将系统时区设置为东八区(北京时间)

[root@db-server ~]# tzselectPlease identify a location so that time zone rules can be set correctly.Please select a continent or ocean.1) Africa2) Americas3) Antarctica4) Arctic Ocean5) Asia6) Atlantic Ocean7) Australia8) Europe9) Indian Ocean10) Pacific Ocean11) none - I want to specify the time zone using the Posix TZ format.#? 5Please select a country.1) Afghanistan 18) Israel 35) Palestine2) Armenia 19) Japan 36) Philippines3) Azerbaijan 20) Jordan 37) Qatar4) Bahrain 21) Kazakhstan 38) Russia5) Bangladesh 22) Korea (North) 39) Saudi Arabia6) Bhutan 23) Korea (South) 40) Singapore7) Brunei 24) Kuwait 41) Sri Lanka8) Cambodia 25) Kyrgyzstan 42) Syria9) China 26) Laos 43) Taiwan10) Cyprus 27) Lebanon 44) Tajikistan11) East Timor 28) Macau 45) Thailand12) Georgia 29) Malaysia 46) Turkmenistan13) Hong Kong 30) Mongolia 47) United Arab Emirates14) India 31) Myanmar (Burma) 48) Uzbekistan15) Indonesia 32) Nepal 49) Vietnam16) Iran 33) Oman 50) Yemen17) Iraq 34) 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: Sun Jan 11 23:31:51 CST 2015.Universal Time is now: Sun Jan 11 15:31:51 UTC 2015.Is the above information OK?1) Yes2) No#? yesPlease enter 1 for Yes, or 2 for 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

注意:tzselect命令只告诉你选择的时区的写法,并不会生效。所以现在它还不是东8区北京时间。你可以在.profile、.bash_profile或者/etc/profile中设置正确的TZ环境变量并导出。 例如在.bash_profile里面设置 TZ='Asia/Shanghai'; export TZ并使其生效。

[root@db-server ~]# source .bash_profile[root@db-server ~]# dateSun Jan 11 23:37:40 CST 2015

 

方法2:复制相应的时区文件,替换系统时区文件;或者创建链接文件

 

 

在/usr/share/zoneinfo/下面有很多时区文件,如下所示,可以复制这些时区文件覆盖/etc/localtime文件,或修改符号链接/etc/locatime对应的文件

[root@db-server ~]# date -RMon, 12 Jan 2015 10:42:26 +0800[root@db-server ~]# cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtimecp: overwrite `/etc/localtime'? y[root@db-server ~]# date -RSun, 11 Jan 2015 18:42:49 -0800[root@db-server ~]#

注意如果有时候,执行了上面命令后,使用date -R发现时区设置没有生效,有可能是因为你在profile或.bash_profile里面设置了TZ,如下案例所示:

[root@db-server ~]# date -RSun, 11 Jan 2015 18:42:49 -0800[root@db-server ~]# vi .bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/binTZ='Asia/Shanghai'; export TZexport PATHunset USERNAME~~".bash_profile" 14L, 221C written[root@db-server ~]# source .bash_profile[root@db-server ~]# date -RMon, 12 Jan 2015 10:44:35 +0800



    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/25855.html

    分享给朋友:

    “linux 时区 java linux的时区” 的相关文章

    CN2专线节点:连接全球的超高速信息高速公路

    在全球化的背景下,企业之间的信息传输和数据交换需求日益增加。而在这个竞争激烈的市场环境中,网络连接的稳定性、速度和安全性成为企业提升竞争力的关键因素之一。CN2专线节点,作为中国移动通信领域的重要组成部分,正是解决这些需求的“利器”。CN2专线节点,全称中国电信ChinaNetNextGenerat...

    全球主机论坛:交流与学习的技术社区

    在现代社会,全球主机论坛的出现为我们提供了一个交流和学习的平台。这个论坛主要聚焦于主机领域,用户可以自由讨论主机的各种话题,分享个人经验,并获取最新的行业信息。对我而言,这样的论坛不仅是一个获取知识的地方,更是一个与全球主机用户互动的社区。 全球主机论坛的重要性毋庸置疑。它为主机使用者提供了一个集中...

    最佳Mac SSH连接工具推荐:轻松管理远程服务器

    随着远程工作和云计算的普及,SSH协议成为了连接服务器和管理远程设备的重要工具。在Mac上,有许多SSH连接工具可供选择,让我们来逐一了解它们的特点和应用场景。 SSH协议简介 SSH,即安全外壳协议,是一种用于安全登录远程主机的网络协议。它提供了一条加密的连接通道,确保数据在传输过程中的安全性。通...

    选择香港主机的最佳指南:提升您的网站性能与用户体验

    香港主机指的是那些在香港地区部署的服务器,主要用于提供网站托管、应用托管或数据库管理等服务。得益于香港卓越的网络基础设施,越来越多的企业和个人选择将他们的运营托付给香港主机。这不仅提升了业务的可达性,也提供了更优质的用户体验。 如果我回想起我最初接触香港主机时,感到非常惊讶于它的潜力。香港地理位置独...

    VPS搭建:从选择提供商到后续管理的全面指南

    什么是VPS搭建? 了解VPS搭建的第一步是弄清楚VPS的定义。VPS,全称为虚拟专用服务器,是将一个物理服务器划分成多个独立的虚拟服务器。每个VPS都具有自己的操作系统和资源,能够像独立服务器一样运行各种应用程序。这种方式提供了更高的灵活性和可控性,相比共享主机来说,用户能够自主安装软件,配置环境...

    揭秘 cheapnat 优惠码的省钱技巧与使用攻略

    什么是 cheapnat 优惠码 我最近发现,cheapnat 优惠码成为了网络用户省钱的绝佳利器。它们通常是特定服务或产品的折扣代码,可以帮助用户在购买 VPS、域名注册或代理服务时享受不同程度的价格优惠。无论你是个人用户还是小型企业,合理利用这些优惠码都能帮你减少开支,同时享受到高质量的服务。...