77

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.

4
  • Currently I am retrieving the time zone information using the GeoTools library and a shapefile provided by the National Atlas of the United States (nationalatlas.gov/mld/timeznp.html). Fortunately this provides me with some additional information - primarily the time zone symbol which is 2 or 4 digits (i.e AL, EA, EAno, etc). Unfortunately this value doesn't correspond to those used by Java time zones although I could perform this mapping manually. Ideally I'd like a solution that would work if I replaced this file with a world time zone shapefile but that might be too ambitious.
    – gelgamil
    Jun 29, 2009 at 21:21
  • timeznp020.txt lists Enumerated_Domains which include details of each abbreviation used; it shouldn't be too difficult to map those to zoneinfo time zone names. I'm not sure if time zone names in Java are cross-platform, but I'd hope so!
    – tc.
    Oct 3, 2011 at 23:52
  • another answer which is useful for this problem stackoverflow.com/a/1449510/311525
    – Scott
    May 7, 2014 at 5:18
  • stackoverflow.com/questions/10545960/…
    – user4761989
    Apr 8, 2015 at 5:09

6 Answers 6

88

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() );
4
  • 1
    I have been working on this same problem. I have not found a shape file for the world time zones.
    – Clint
    Jun 30, 2009 at 0:25
  • We don't know if this is a desktop app or a web app, though. A Web app wouldn't have access to the time zone information on the client.
    – Powerlord
    Jun 30, 2009 at 13:55
  • Android: Requires API level 24 Mar 3, 2017 at 21:30
  • Since java 8, it's better to use zoneid instead, check Basil Bourque answer which the correct post java 8 answer.
    – Chris
    Dec 21, 2022 at 18:00
65

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 Answer by mamboking.

Example code:

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

Note how in the last line we had to extract an Instant object from our ZonedDateTime object with a simple call to toInstant.

Table of date-time types in Java, both modern and legacy.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

0
10
TimeZone tz = TimeZone.getTimeZone("EST");
boolean inDs = tz.inDaylightTime(new Date());
2
  • 8
    Three later abbreviations like EST (meant for standard times) should be avoided as they are DST-insensible. Use a time zone id instead.
    – Tiny
    Jan 15, 2016 at 19:51
  • @Tiny It’s even more confusing than that: some of them are DST sensitive, some are not. Every reason to avoid them. (In 2021 we should avoid the old-fashioned TimeZone class too and use ZoneId).
    – Anonymous
    Jul 11, 2021 at 17:43
3

You're going to have to do a bit more work using those coordinates and figure out which time zone they're in. Once you know which TimeZone that is, the isDayLight() method would be useful.

For example, you have no way of telling whether -0500 is EST (US/Canada Eastern Standard Time), CDT (US/Canada Central Daylight Time), COT (Colombia Time), AST (Brazil Acre Standard Time), ECT (Ecuador Time), etc...

Some of these may or may not support daylight saving time.

1

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

To supplement this, you will need to look up the current time zone with your latitude/longitude info. GeoNames provides a java client for its web service, as well as a simple web-request framework (i.e. http://ws.geonames.org/timezone?lat=47.01&lng=10.2)

1
  • I've looked into the GeoNames web service previously and I am aware it will accomplish what I am trying to do. Unfortunately this code will be hosted on servers that may be located behind a firewall so I'm trying to avoid external web service calls if possible. For those without this restriction the GeoNames web service would be a great solution.
    – gelgamil
    Jun 29, 2009 at 21:34
0
  1. I'm not sure what you'd be doing with the offset, but the getOffset() method of the TimeZone class will get the offset, without having to find out whether the time being processed is in daylight saving time or not - it does all that work for you. You may even be able to just use the ZonedDateTime class.
  2. There is a project that generates a shapefile of IANA timezone boundaries, which might let you map from latitude/longitude to a timezone ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.