如何更改 java.util.Calendar/Date 的 TIMEZONE [英] How to change TIMEZONE for a java.util.Calendar/Date

查看:20
本文介绍了如何更改 java.util.Calendar/Date 的 TIMEZONE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时更改 Java 日历实例中的 TIMEZONE 值.我在下面试过.但两种情况下的输出是相同的:

I would like to change the TIMEZONE value in a Java Calendar instance at runtime. I tried below. But the output is the same in both instances:

    Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    System.out.println(cSchedStartCal.getTime().getTime());
    cSchedStartCal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
    System.out.println(cSchedStartCal.getTime().getTime());

输出:
1353402486773
1353402486773

OUTPUT:
1353402486773
1353402486773

我也试过了,但输出还是一样:

I have tried this also but the output is still the same:

    Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    System.out.println(cSchedStartCal.getTime());

    Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
    cSchedStartCal1.setTime(cSchedStartCal.getTime());
    System.out.println(cSchedStartCal.getTime());

在 API 中,我看到了下面的评论,但我无法理解其中的大部分内容:

In the API I am seeing the below comment but I am not able to understand much of it:

     * calls: cal.setTimeZone(EST); cal.set(HOUR, 1); cal.setTimeZone(PST).
     * Is cal set to 1 o'clock EST or 1 o'clock PST?  Answer: PST.  More
     * generally, a call to setTimeZone() affects calls to set() BEFORE AND
     * AFTER it up to the next call to complete().

你能帮我吗?

一种可能的解决方案:

    Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    long gmtTime = cSchedStartCal.getTime().getTime();

    long timezoneAlteredTime = gmtTime + TimeZone.getTimeZone("Asia/Calcutta").getRawOffset();
    Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
    cSchedStartCal1.setTimeInMillis(timezoneAlteredTime);

这个解决方案好吗?

推荐答案

在 Java 中,日期在内部表示为自纪元以来的 UTC 毫秒(因此不考虑时区,这就是为什么您得到与 getTime() 为您提供提到的毫秒数.
在您的解决方案中:

In Java, Dates are internally represented in UTC milliseconds since the epoch (so timezones are not taken into account, that's why you get the same results, as getTime() gives you the mentioned milliseconds).
In your solution:

Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long gmtTime = cSchedStartCal.getTime().getTime();

long timezoneAlteredTime = gmtTime + TimeZone.getTimeZone("Asia/Calcutta").getRawOffset();
Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
cSchedStartCal1.setTimeInMillis(timezoneAlteredTime);

您只需将 GMT 的偏移量添加到指定的时区(在您的示例中为亚洲/加尔各答"),以毫秒为单位,因此这应该可以正常工作.

you just add the offset from GMT to the specified timezone ("Asia/Calcutta" in your example) in milliseconds, so this should work fine.

另一种可能的解决方案是利用 Calendar 类的静态字段:

Another possible solution would be to utilise the static fields of the Calendar class:

//instantiates a calendar using the current time in the specified timezone
Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
//change the timezone
cSchedStartCal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
//get the current hour of the day in the new timezone
cSchedStartCal.get(Calendar.HOUR_OF_DAY);

参考 stackoverflow.com/questions/7695859/ 更深入的解释.

Refer to stackoverflow.com/questions/7695859/ for a more in-depth explanation.

这篇关于如何更改 java.util.Calendar/Date 的 TIMEZONE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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