Get daylight saving time for time zones in Java

I would like to find out the easiest way in Java to get a list of dates in the future when daylight saving time changes.

One rather inelegant way to do this is to simply iterate over several days a day, testing them against TimeZone.inDaylightTime (). This will work, and I'm not worried about efficiency, since it will need to be launched every time my application starts, but I am wondering if there is an easier way.

If you're wondering why I am doing this, this is because I have a javascript application that needs to process third-party data containing UTC timestamps. I want a reliable way to translate from GMT to EST on the client side. See Javascript - Unix time for a specific time zone. I wrote some javascript that will do this, but I want to get the exact dates of the transition from the server.

+18
java date dst
Sep 19 '09 at 20:55
source share
2 answers

Joda Time (as always) makes this very easy due to DateTimeZone.nextTransition . For example:

 import org.joda.time.*; import org.joda.time.format.*; public class Test { public static void main(String[] args) { DateTimeZone zone = DateTimeZone.forID("Europe/London"); DateTimeFormatter format = DateTimeFormat.mediumDateTime(); long current = System.currentTimeMillis(); for (int i=0; i < 100; i++) { long next = zone.nextTransition(current); if (current == next) { break; } System.out.println (format.print(next) + " Into DST? " + !zone.isStandardOffset(next)); current = next; } } } 

Output:

 25-Oct-2009 01:00:00 Into DST?  false
 28-Mar-2010 02:00:00 Into DST?  true
 31-Oct-2010 01:00:00 Into DST?  false
 27-Mar-2011 02:00:00 Into DST?  true
 30-Oct-2011 01:00:00 Into DST?  false
 25-Mar-2012 02:00:00 Into DST?  true
 28-Oct-2012 01:00:00 Into DST?  false
 31-Mar-2013 02:00:00 Into DST?  true
 27-Oct-2013 01:00:00 Into DST?  false
 30-Mar-2014 02:00:00 Into DST?  true
 26-Oct-2014 01:00:00 Into DST?  false
 29-Mar-2015 02:00:00 Into DST?  true
 25-Oct-2015 01:00:00 Into DST?  false
 ...

With Java 8, you can get the same information using ZoneRules using the nextTransition and previousTransition methods.

+31
Sep 19 '09 at 21:00
source share

java.time

The modern answer uses java.time, the modern Java date and time API.

  ZoneId zone = ZoneId.of("Europe/London"); ZoneRules rules = zone.getRules(); ZonedDateTime now = ZonedDateTime.now(zone); ZoneOffsetTransition transition = rules.nextTransition(now.toInstant()); Instant max = now.plusYears(15).toInstant(); while (transition != null && transition.getInstant().isBefore(max)) { System.out.println(transition); transition = rules.nextTransition(transition.getInstant()); } 

Conclusion, in abbreviated form:

 Transition[Overlap at 2019-10-27T02:00+01:00 to Z] Transition[Gap at 2020-03-29T01:00Z to +01:00] Transition[Overlap at 2020-10-25T02:00+01:00 to Z] Transition[Gap at 2021-03-28T01:00Z to +01:00] Transition[Overlap at 2021-10-31T02:00+01:00 to Z] Transition[Gap at 2022-03-27T01:00Z to +01:00] Transition[Overlap at 2022-10-30T02:00+01:00 to Z] (cut) Transition[Overlap at 2033-10-30T02:00+01:00 to Z] Transition[Gap at 2034-03-26T01:00Z to +01:00] 

I would not trust too much data. I'm not sure what happens in the UK in the aftermath of Brexit (and after the EU can give up Daylight Saving Time (DST) in 2021).

Link: Oracle, tutorial: Date and time explaining how to use java.time.

0
Jun 17 '19 at 9:54 on
source share



All Articles