I realize that you can check if a Date
is inDaylightTime
, but how can I determine when (or if) that Date switches to and from DST? I can iterate over every day and check if it's inDaylightTime, but is there a cleaner solution/method?
2 Answers
I would suggest using Joda Time - its DateTimeZone
class has nextTransition
and previousTransition
methods.
Note that transitions can occur for reasons other than DST - a change in standard time, or perhaps a change in the name of the zone interval. But that's how you'd find out, anyway :)
(Joda Time is a much better date and time library to start with, to be honest.)

- 1,421,763
- 867
- 9,128
- 9,194
-
+1 and I would love to use a different library, but I should have mentioned I'm working with legacy code and have to use the Java Date and DateTime :-\ – Igor Nov 08 '11 at 16:26
-
@GrailsDev: Even if you're not going to use Joda Time elsewhere, can you not introduce it *even for this bit*? (I can't see anything which will give you transitions in the Java standard classes, even for `SimpleTimeZone`.) – Jon Skeet Nov 08 '11 at 16:27
-
I suppose I can... was wondering more if it's possible in the default Java class. – Igor Nov 08 '11 at 16:29
-
1@GrailsDev: Not that I can see. Joda Time makes it pretty easy to convert from a Java `Date` though. Maybe you can introduce it throughout the project by stealth ;) – Jon Skeet Nov 08 '11 at 16:29
-
Any way to do it without using Joda ? in Java version less than 8. – DevilCode Sep 08 '16 at 23:16
-
@DevilCode: Not that I'm aware of in an efficient way. See https://github.com/nodatime/tzvalidate/blob/master/java/org/nodatime/tzvalidate/Java7Dump.java#L67 for some code that works, but only by testing every day... – Jon Skeet Sep 09 '16 at 05:32
java.time
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice from the home page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
Solution using java.time
, the modern Date-Time API: Use ZoneRules#nextTransition
and ZoneRules#previousTransition
.
Demo:
import java.time.Instant;
import java.time.ZoneId;
import java.time.zone.ZoneRules;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
ZoneRules zoneRules = ZoneId.of("Europe/London").getRules();
System.out.println(zoneRules.nextTransition(now));
System.out.println(zoneRules.previousTransition(now));
}
}
Output:
Transition[Overlap at 2021-10-31T02:00+01:00 to Z]
Transition[Gap at 2021-03-28T01:00Z to +01:00]
You can understand the output from the following images:
From the image, you can see that on 28 Mar 2021, after 00:59, the time changed to 02:00 i.e. there was no 01:00 that night. Is there a way to know the local time and the zone offset just before the transition happened? Check ZoneOffsetTransition
or the following demo for the answer.
What were the local time and the zone offset before the transition?
import java.time.Instant;
import java.time.ZoneId;
import java.time.zone.ZoneOffsetTransition;
public class Main {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.of("Europe/London");
ZoneOffsetTransition nextTransition = zoneId.getRules().previousTransition(Instant.now());
System.out.println("Local date-time before the transition: " + nextTransition.getDateTimeBefore());
System.out.println("Zone offset before the transition: " + nextTransition.getOffsetBefore());
}
}
Output:
Local date-time before the transition: 2021-03-28T01:00
Zone offset before the transition: Z
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

- 71,965
- 6
- 74
- 110