• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar - TimeZone  RSS feed

 
Ayondeep Datta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had this weird problem with Calendar time zone today. I have two calendars set to same date ("7/20/2009"). The first one has default timezone (Central Standard Time). The second one I am explicitly setting the time zone to "Central Standard Time". Now when I do a getTimeInMillis() I get different results . The first one returns "1248066000000" and the second one returns "1248048000000" .

Calendar myCal1 =Calendar.getInstance();
myCal1.setTime(changeEffectiveDate);
myCal1.set(Calendar.HOUR_OF_DAY, 0);
myCal1.set(Calendar.MINUTE, 0);
myCal1.set(Calendar.SECOND, 0);
myCal1.set(Calendar.MILLISECOND, 0);
myCal1.getTimeInMillis();

Calendar myCal2 =Calendar.getInstance();
myCal2.setTime(changeEffectiveDate);
myCal2.setTimeZone("Central Standard Time");
myCal2.set(Calendar.HOUR_OF_DAY, 0);
myCal2.set(Calendar.MINUTE, 0);
myCal2.set(Calendar.SECOND, 0);
myCal2.set(Calendar.MILLISECOND, 0);
myCal2.getTimeInMillis();

Here changeEffectiveDate is an instance of Date set to "7/20/2009". I havetested it on JDK1.4 as well as JDK1.6 with same result. Anyone has any idea?

Thanks,
Ayondeep
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps "Central Standard Time" isn't the name of any timezone, and some default is being used instead. You could examine the Calendar object after you set that, to see what its actual timezone is.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's it exactly. "Central Standard Time" is not recognized by TimeZone.getTimeZone() (you are using that, right? Your code doesn't compile as written.) Unfortunately TimeZone.getTimeZone() does not do the sensible thing when it encounters an unrecognized time zone - it just returns GMT instead. Yet another example of bad design in the Java date/time classes.

Here's a useful way to get a list of the strings getTimeZone() recognizes:

Stay away from the three-letter abbreviations, other than GMT - they are dangerous and unreliable. For Central Time (US), I recommend America/Chicago, unless you see another that seems more appropriate. Note that you're not actually using Central Standard Time right now; you're using Central Daylight Time.
 
Ayondeep Datta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Yes, you guys are right. After examining my calendar object, it seems it didn't recognize "Central Standard Time" and set it to "GMT" by default.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!