[thelist] Java question: getTimezoneOffset() Deprecated

Hassan Schroeder hassan at webtuitive.com
Tue Dec 30 06:37:16 CST 2003


John C Bullas wrote:

> Here is an example line of a clock program that a buddy recompiled after 
> some mods (not this line).
> 
> private static final int LOCALTIMEZONEOFFSET = (new 
> Date()).getTimezoneOffset();
> 
> I[using j2sdk1.4.2 and the line I got back from the compiler was 
> shocking. ]
> 
> C:\Java\clock>javac Clock.java
> Note: Clock.java uses or overrides a deprecated API.
> Note: Recompile with -deprecation for details.
> 
> I don't know what it means :-)) 

It means that at some point, with a future JDK, your clock program
won't compile, though it's OK now :-)

> Okay, now to the serious bit of it all
> 
> This is what I've read in the API docs:
> .../java/util/Date.html#getTimezoneOffset()>getTimezoneOffset() 
> 
>           Deprecated. As of JDK version 1.1, replaced by 
> -(Calendar.get(Calendar.ZONE_OFFSET) + 
> Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000).
> 
> Well, that's easy peasy.
> 
> Now the line of code looks like this:

> private static final int LOCALTIMEZONEOFFSET = 
> (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) 
> / (60*1000);
> 
> But that actually does not compile. 

First you have to instantiate a Calendar object.

Replace that one line with these two --

   private static final Calendar cal = new GregorianCalendar();
	
   private static final int LOCALTIMEZONEOFFSET =
     (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60*1000);


HTH!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.





More information about the thelist mailing list