在JAVA中使用Date类处理TimeZone更改 [英] Handling TimeZone change with Date class in JAVA

查看:339
本文介绍了在JAVA中使用Date类处理TimeZone更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试如下打印当前系统日期和时间,

 公共类PrintDate {

public void getDate(){
while(true){
System.out.println(new Date());
}
}

public static void main(String [] args){
new PrintDate()。getDate();
}
}

此无限循环按预期方式打印当前系统时间戳



示例:


$ b,并且当我在操作系统中更改日期或时间但不更改时区时,它工作正常。



$ b


  1. 我启动了上面的代码,该代码按预期连续打印当前系统时间戳。

  2. 当我更改系统日期或时间时,它成功地反映在代码中。

  3. 当我更改系统时区时,它没有反映在代码中。自程序启动以来,它仍然显示相同的时区。

我可以知道其背后的原因吗?

解决方案

neuhaus的答案是正确的。


如果您要在运行代码时更改主机操作系统的时区,请知道Java虚拟机(JVM)具有其自己的当前默认时区。


通常,该默认值是从主机OS的默认值中获取的。如果是这种情况,则必须表示Java实现仅在启动时检测主机时区,而不检查主机OS时区的以后更改。


您的JVM也可以在启动时设置为配置参数。在那种情况下,我应该认为JVM会故意忽略主机OS的时区更改。


该JVM中任何应用程序的任何线程中的任何Java代码都可以更改JVM的当前默认时区。在运行时的任何时候。同样,我应该认为JVM会故意忽略主机OS的时区更改。


java.util.TimeZone.getDefault() 概述了确定当前默认时区的步骤。


如果缓存的默认TimeZone可用,则返回其克隆。否则,该方法将按照以下步骤确定默认时区。


•使用 user.timezone 属性值作为默认值时区ID(如果有)。


•检测平台时区ID。平台时区和ID映射的来源可能因实现方式而异。


•如果给定或检测到的时区ID未知,则将GMT作为最后的手段。


将从ID创建的默认TimeZone缓存,并返回其克隆。 user.timezone 属性值在返回时设置为ID。


也就是说,JVM不会检测到主机操作系统设置的任何更改。启动后,一旦确定默认值,它就会存储在该 user.timezone 属性(以及缓存中的值)中,直到通过调用 setDefault


java.time


您正在使用旧的java.util.Date类,该类具有已被 java.time框架在Java 8中。


使用 java.time.ZonedDateTime 类,并指定所需/期望的时区。

  ZoneId zoneId = ZoneId.of( America / Montreal); 
ZonedDateTime zdt = ZonedDateTime.now(ZoneId);

您几乎应该永远不要依赖JVM的当前默认时区(也不要依赖当前默认的 Locale )。


I am trying to print the current system date and time as below,

public class PrintDate {

    public void getDate(){
        while(true){
            System.out.println(new Date());
        }
    }

    public static void main(String[] args) {
       new PrintDate().getDate();
    }
}

This endless loop prints the current system time stamp as expected and it works fine when i make change in date or time in the OS but not with the timezone change..

Example :

  1. I started the above code , which continuously print the current system time stamp as expected.
  2. When i change the system date or time , it successfully gets reflected in the code.
  3. When i change the system timezone , It is not reflecting in the code. It still show the same timezone since the program started.

May i know the reason behind this?

解决方案

The answer by neuhaus is correct.

If you meant you changed the time zone of your host operating system while running that code, know that the Java Virtual Machine (JVM) has its own current default time zone.

Usually that default is picked up from that of the host OS. If so in your case, that must mean your Java implementation is detecting the host time zone only at launch and not checking for later changes in the host OS‘ time zone.

The time zone of your JVM can also be set as a configuration parameter upon launch. In that case I should think the JVM would purposely ignore the host OS’ time zone changes.

Any Java code in any thread of any app within that JVM can change the JVM’s current default time zone at any moment during runtime. Again, I should think the JVM would purposely ignore the host OS’ time zone changes.

The class doc for java.util.TimeZone.getDefault() outlines steps taking in determining the current default time zone.

If the cached default TimeZone is available, its clone is returned. Otherwise, the method takes the following steps to determine the default time zone.

• Use the user.timezone property value as the default time zone ID if it's available.

• Detect the platform time zone ID. The source of the platform time zone and ID mapping may vary with implementation.

• Use GMT as the last resort if the given or detected time zone ID is unknown.

The default TimeZone created from the ID is cached, and its clone is returned. The user.timezone property value is set to the ID upon return.

In my reading of that, it says the JVM is not going to detect any changes to the host OS setting. Once launched, and once a default has been determined, it is stored in that user.timezone property (and a value in cache) until changed with a call to setDefault.

java.time

You are using the old java.util.Date class which has been supplanted by the java.time framework in Java 8.

Use the java.time.ZonedDateTime class and specify the desired/expected time zone.

ZoneId zoneId = ZoneId.of( " America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( ZoneId );

You should almost never depend on the JVM’s current default time zone ( nor the current default Locale).

这篇关于在JAVA中使用Date类处理TimeZone更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆