简体   繁体   English

Hotspot JVM如何确定Linux(Centos)上的当前时区?

[英]How does Hotspot JVM determine exactly the current timezone on Linux(Centos)?

I have a problem when the current timezone is not determined correctly in a JVM instance running inside a Docker container (both host and container are Cent OS 6.5). 如果在Docker容器内运行的JVM实例中未正确确定当前时区(主机和容器都是Cent OS 6.5),则会出现问题。

First of all I have mapped etc/localtime to the child container via -v /etc/localtime:/etc/localtime:ro (I think it's a prevalent way ) 首先,我已映射etc/localtime通过子容器-v /etc/localtime:/etc/localtime:ro (我认为这是一个普遍的方式

When I login via SSH both on host and container 当我通过SSH登录主机和容器时

date -u 日期-u

prints Mon Apr 20 11:48:57 UTC 2015 打印Mon Apr 20 11:48:57 UTC 2015

and

date 日期

prints Mon Apr 20 14:50:41 MSK 2015 打印Mon Apr 20 14:50:41 MSK 2015

In JVM however with 但是在JVM中

System.out.println(new Date()); System.out.println(new Date());

I get Mon Apr 20 11:52:24 UTC 2015 inside container and Mon Apr 20 14:53:17 MSK 2015 inside the host. 我将在Mon Apr 20 11:52:24 UTC 2015内部容器和Mon Apr 20 14:53:17 MSK 2015内部主机内部。

How is the current timezone determined exactly? 目前的时区如何确定?

Oracle FAQ did not shed the light, I don't quite understand what metrics Java does Java use to get timezone for the current user Oracle常见问题解答没有说明,我不太清楚Java使用什么指标来为当前用户获取时区

Do my operating system's timezone patches fix the Java platform's timezone data? 我的操作系统的时区补丁是否修复了Java平台的时区数据?

No. The Java SE platform's timezone data is not read from the local or host operating system. 不可以。不会从本地或主机操作系统读取Java SE平台的时区数据。 The Java SE platform maintains a private repository of timezone data in locally installed files ( .../jre/lib/zi) as part of the Java Runtime Environment (JRE) software. Java SE平台在本地安装的文件(... / jre / lib / zi)中维护时区数据的私有存储库,作为Java运行时环境(JRE)软件的一部分。 Applying whatever operating system timezone patches (for example Solaris OS, Linux, Windows) will have no effect on the accuracy of the Java SE platform's timezone data. 应用任何操作系统时区修补程序(例如Solaris OS,Linux,Windows)都不会影响Java SE平台时区数据的准确性。

update: if someone is interested in workaround - I've specified TZ environment variable as in Stephen's answer, so now the container is created with parameters 更新:如果有人对解决方法感兴趣 - 我已经在Stephen的答案中指定了TZ环境变量,那么现在使用参数创建容器

-v /etc/localtime:/etc/localtime:ro -e "TZ=Europe/Moscow" -v / etc / localtime:/ etc / localtime:ro -e“TZ = Europe / Moscow”

According to this page , a JVM running on Linux uses the TZ environment variable to give it the name of the local time zone. 根据此页面 ,在Linux上运行的JVM使用TZ环境变量为其指定本地时区的名称。

The page goes on to explain that TZ is normally set in "/etc/profile", and it doesn't work if Java is launched using a mechanism that doesn't "source" that file. 该页面继续解释TZ通常设置在“/ etc / profile”中,如果使用不“源”该文件的机制启动Java,则它不起作用。


The timezone data that the Oracle FAQ refers to is something different. Oracle FAQ引用的时区数据是不同的。 It is the data that the JVM uses to map from timezone names to the corresponding zone offsets (taking account of daylight saving adjustments, etcetera). 它是JVM用于从时区名称映射到相应区域偏移的数据(考虑夏令时调整等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM