Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

GregorianCalendar time not updating when changing TimeZone

Thread view: 
jstorta - 14 May 2007 20:47 GMT
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();
   }
}
========================================
Filip Larsen - 14 May 2007 22:51 GMT
> 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,
Signature

Filip Larsen



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.