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