Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date

I have a Java class that takes in the latitude/longitude of a location and returns the GMT offset when daylight savings time is on and off. I am looking for an easy way to determine in Java if the current date is in daylight savings time so I can apply the correct offset. Currently I am only performing this calculation for U.S. timezones although eventually I would like to expand this to global timezones as well.

like image 500
gelgamil Avatar asked Jun 29 '09 20:06

gelgamil


People also ask

How do you know if a time zone is eligible for daylight savings in Java?

How to identify if a timezone is eligible for DayLight Saving? Explanation: public abstract boolean useDaylightTime() is provided in TimeZone class. 6.

How does Java determine Daylight Savings?

The observesDaylightTime() method of TimeZone class in Java is used to check and verify whether the given date is in daylight saving time or if any transition from Standard Time to Daylight Saving Time will occur at any future time.

How do I check my DST time?

Test transition of DST, i.e. when you are currently in summer time, select a time value from winter. Test boundary cases, such as a timezone that is UTC+12, with DST, making the local time UTC+13 in summer and even places that are UTC+13 in winter.

How do you know if DST is in effect?

To check if DST (Daylight Saving time) is in effect:Check if the greater of the two values is not equal to the offset for the original date.


1 Answers

This is the answer for the machine on which the question is being asked:

TimeZone.getDefault().inDaylightTime( new Date() ); 

A server trying to figure this out for a client will need the client's time zone. See @Powerlord answer for the reason why.

For any particular TimeZone

TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() ); 
like image 149
Clint Avatar answered Sep 17 '22 17:09

Clint