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

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

问题描述

我想在运行时更改Java Calendar实例中的TIMEZONE值。
我试过下面。但是输出在两种情况下是一样的:

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

OUTPUT:

1353402486773

1353402486773



我也试过了,但输出仍然是一样的:

  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中我看到以下注释,但我不能理解大部分: / p>

  * calls:cal.setTimeZone(EST); cal.set(HOUR,1); cal.setTimeZone(PST)。 
*校准设置为1点EST或1点PST?答:PST。更多
*一般来说,调用setTimeZone()会影响set()BEFORE和
*之后的调用,直到下一次调用complete()。你能帮我吗?



一种可能的解决方案:

 日历cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone ); 
long gmtTime = cSchedStartCal.getTime()。getTime();

long timezoneAlteredTime = gmtTime + TimeZone.getTimeZone(Asia / Calcutta)。getRawOffset();
Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone(Asia / Calcutta));
cSchedStartCal1.setTimeInMillis(timezoneAlteredTime);这个解决方案是否正确?



$ b <解决方案

在Java中,Dates在自纪元以来以UTC毫秒为单位进行内部表示(因此时区不被考虑,这就是为什么你得到相同的结果,因为 getTime / code>为您提供的毫秒数)。

在您的解决方案中:

  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的偏移量添加到指定的时区(在示例中为Asia / Calcutta在毫秒,所以这应该工作正常。



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

  //使用指定时区中的当前时间实例化日历
Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone(GMT)) ;
//更改时区
cSchedStartCal.setTimeZone(TimeZone.getTimeZone(Asia / Calcutta));
//获取新时区的当前时间
cSchedStartCal.get(Calendar.HOUR_OF_DAY);

请参阅 stackoverflow.com/questions/7695859/ ,以获得更深入的解释。


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());

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());

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

Could you please help me?

One Possible 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);

Is this solution ok?

解决方案

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

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

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

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

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

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