GregorianCalendar time not updating when changing TimeZone

J

jstorta

I am having an issue trying to update the timezone on a
GregorianCalendar instance. I tried this code sample on a couple of
different systems, all with the same result.

This code basically does four things.

1) Create an instance of GregorianCalendar in the "GMT" timezone with
the date/time set to May 14, 2007 17:30
2) Print the HOUR_OF_DAY and MINUTE of the instance along with
toString()
3) Update the timezone to "America/New_York" using the setTimeZone
method.
4) Print the HOUR_OF_DAY and MINUTE of the instance along with
toString()

The code, as it is written below, works fine and prints exactly what I
would expect it to. The output of the toString() calls look correct
in both cases.

The problem is, if I remove the first call to
System.out.println( myCal.get(Calendar.HOUR_OF_DAY) + ":" +
myCal.get(Calendar.MINUTE) );

Then the HOUR and HOUR_OF_DAY fields are never updated. The timezone
field is updated fine. The zoneoffest is populated corrected, and the
dstoffest is updated correctly to reflect the new timezone. But the
actual HOUR and HOUR_OF_DAY fields are not changed.

It is like I have to retrieve the value of a field in the instance
before the fields will update.

I've included the code that works and the code that does not work.
Any guidance would be appreciated.

Thanks.


This code works
========================================
import java.util.*;

public class MyDate {
public MyDate() {
GregorianCalendar myCal = new
GregorianCalendar( TimeZone.getTimeZone( "GMT" ) );
myCal.set( 2007, 4, 14, 17, 30 );
System.out.println( myCal.get(Calendar.HOUR_OF_DAY) + ":" +
myCal.get(Calendar.MINUTE) );

myCal.setTimeZone( TimeZone.getTimeZone( "America/New_York" ) );
System.out.println( myCal.get(Calendar.HOUR_OF_DAY) + ":" +
myCal.get(Calendar.MINUTE) );
}

public static void main( String... args ) {
MyDate testDate = new MyDate();
}
}
========================================

In this code, the hour fields are never updated. The only difference
is the one line that is commented out.
========================================
import java.util.*;

public class MyDate {
public MyDate() {
GregorianCalendar myCal = new
GregorianCalendar( TimeZone.getTimeZone( "GMT" ) );
myCal.set( 2007, 4, 14, 17, 30 );
//System.out.println( myCal.get(Calendar.HOUR_OF_DAY) + ":" +
myCal.get(Calendar.MINUTE) );

myCal.setTimeZone( TimeZone.getTimeZone( "America/New_York" ) );
System.out.println( myCal.get(Calendar.HOUR_OF_DAY) + ":" +
myCal.get(Calendar.MINUTE) );
}

public static void main( String... args ) {
MyDate testDate = new MyDate();
}
}
========================================
 
F

Filip Larsen

jstorta said:
I am having an issue trying to update the timezone on a
GregorianCalendar instance. I tried this code sample on a couple of
different systems, all with the same result.

This is a side effect of how the Calendar class works. You can set
timezone before or after you set the time using Calendar.set(int). Only
when you use Calendar.get(int) (and a few other query methods that call
the protected method Calendar.complete()) will the time set by
Calendar.set(int) and the timezone be combined into a millisecond time.

So your call to Calendar.get between setting the two timezones is
important. If you leave it out you are in effect using the last timezone
for the time you set.


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top