This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes java.util.TimeZone question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.util.TimeZone question" Watch "java.util.TimeZone question" New topic

java.util.TimeZone question

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41182
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.

Ping & DNS - my free Android networking tools app
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41182
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

Joined: Mar 22, 2005
Posts: 41182
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 ]
I agree. Here's the link:
subject: java.util.TimeZone question
Similar Threads
abstract method TimeZone.getRawOffset
Illegal modifier for parameter y, only final is permitted
ClassCastException - SimpleTimeZone
RMI and Oracle's Jserver VM
java.util.Date version conflict