我想在运行时更改 Java 日历实例中的 TIMEZONE 值。我在下面试过。但是两种情况下的输出是相同的:
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
我也试过这个,但输出仍然是一样的:
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 中,我看到了以下评论,但我无法理解其中的大部分内容:
* 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);
这个解决方案好吗?