Determine if Summer Saving Time (DST) is active in Java for a specified date

I have a Java class that takes a latitude / longitude location and returns a clockwise offset when daylight saving time is turned on and off. I am looking for an easy way to determine in Java if the current date is in summer, so I can apply the correct offset. Currently, I am only performing this calculation for US time zones, although I would like to extend it to global time zones in the end.

+51
java dst datetimeoffset
Jun 29 '09 at 20:52
source share
5 answers

This is the answer for the machine asked the question:

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

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

For any particular TimeZone

 TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() ); 
+64
Jun 29 '09 at 20:57
source share

TL; dr

 ZoneId.of( "America/Montreal" ) // Represent a specific time zone, the history of past, present, and future changes to the offset-from-UTC used by the people of a certain region. .getRules() // Obtain the list of those changes in offset. .isDaylightSavings( // See if the people of this region are observing Daylight Saving Time at a specific moment. Instant.now() // Specify the moment. Here we capture the current moment at runtime. ) // Returns a boolean. 

java.time

Here is the modern java.time (see Tutorial ) version of the correct mamboking answer .

Code example:

 ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) ); … ZoneId z = now.getZone(); ZoneRules zoneRules = z.getRules(); Boolean isDst = zoneRules.isDaylightSavings( now.toInstant() ); 

Notice that on the last line, we had to extract the Instant object from our ZonedDateTime object with a simple call toInstant .




About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete date and time classes, such as java.util.Date , Calendar , and SimpleDateFormat .

The Joda-Time project, currently in maintenance mode , recommends switching to the java.time classes.

To learn more, see the Oracle Tutorial . And a search for many examples and explanations. JSR 310 specification .

You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.* Needed.

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a testing ground for possible future additions to java.time. Here you can find some useful classes such as Interval , YearWeek , YearQuarter and others .

+24
Sep 11 '15 at 16:47
source share
 TimeZone tz = TimeZone.getTimeZone("EST"); boolean inDs = tz.inDaylightTime(new Date()); 
+10
Jun 29 '09 at 20:58
source share

You will need to work a bit with these coordinates and find out what time zone they are in. Once you know what TimeZone it is, the isDayLight () method would be useful.

For example, you don’t have a way to find out if E-mail (-0500) EST (Eastern Standard Time USA / Canada), CDT (US / Canada, Central Daylight Time), COT (Colombian Time), AST (Standard Time Brazil Acre ) ECT (Ecuador time), etc.

Some may or may not support daylight saving time.

+3
Jun 29 '09 at 21:03
source share

Joda Time contains processing methods that will calculate the offsets for you. See DateTimeZone.convertLocalToUTC (...)

In addition to this, you will need to find the current time zone with latitude / longitude information. GeoNames provides a java client for its web service, as well as a simple web request structure (i.e. http://ws.geonames.org/timezone?lat=47.01&lng=10.2 )

+1
Jun 29 '09 at 21:06
source share



All Articles