Tuesday, April 18, 2006

Java Timezone class

I was looking at java.util.TimeZone class. I guessed this could be important in places like the USA where there are three or more timezones.

However in addition to being usefull for places with different timezones it also seems to cater to places with daylight savings time. For the uninformed, especially from places like india where there is no daylight savings this link gives a lot of information :-

http://en.wikipedia.org/wiki/Daylight_saving_time

The javadoc is at :-

http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimeZone.html

The interesting methods to use are the getOffset Methods :-

public abstract int getOffset(int era,int year,int month,int day,int dayOfWeek,int milliseconds)

Gets the time zone offset, for current date, modified in case of daylight savings. This is the offset to add to UTC to get local time.
This method returns a historically correct offset if an underlying TimeZone implementation subclass supports historical Daylight Saving Time schedule and GMT offset changes.

One can use this method in this fashion :-

TimeZone tz = timeZone.getTimeZone("America/Los_Angeles");
long actualDate = (new Date().getTime() ) + timeZone.getOffset(new Date().getTime() )

0 Comments:

Post a Comment

<< Home