Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.TimeZone question

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working with some quite old code, which makes the assumption that "TimeZone.getDefault()" returns an instance of java.util.SimpleTimeZone. That may have been true for some JVMs (e.g. the old Apple MRJ), but it's not true now on OS X. And according to the javadocs, one shouldn't make assumptions about the class returned by getDefault(). (The class it returns is in the sun.* hierarchy, which I can't use since this is an applet.)

The reason it matters is because the code does some advanced calculations with timezones, daylight savings time and the like, and it uses the SimpleTimeZone.getRawOffset method. And I can't seem to create an instance of SimpleTimeZone directly, because for that one needs the raw offset to begin with.

Now, I know that the timezone is the one where the user is located, so how do I do construct a local SimpleTimeZone? Or, which seems to amount to the same thing, how do I calculate the local raw offset?

Thanks in advance for any clues.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't getRawOffset() defined as an abstract method in TimeZone? You should be able to access this for any TimeZone, not just SimpleTimeZone. You may also benefit from useDaylightTime() and getDSTSavings() - or just use getOffset(). Unfortunately there isn't really an easy way to learn the start and end dates for DST from a TimeZone, so if you need that, you might need to go to a trial-and-error approach. Note that this can vary from year to year.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drat. You're right. I could have sworn that I tried that first, and that the compiler complained about it. Works fine now - thanks.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were wondering what this was about, I noticed that no functioning copies of Martin Minow's SunSphere applet still seemed to be out there. So I reactivated it here.

It was prompted by some other thread mentioning that the earth rotates "faster" around the equator than it does near the poles.
[ July 26, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic