java如何调用date类,在JAVA中使用Date类处理TimeZone更改

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 :

I started the above code , which continuously print the current system time stamp as expected.

When i change the system date or time , it successfully gets reflected in the code.

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?

解决方案

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值