LocalTime, ZonedDateTime and Daylight Saving Time in Java
Times of day and time zones in java.time: LocalTime and LocalDateTime, ZoneId and the IANA database, converting between zones and instants, gaps and overlaps at daylight saving changes, ZoneRules, Duration versus Period across transitions, and when to use OffsetDateTime.
kvi5hnuCode Sprout team member10 min readOn this page
Article 68 dealt with calendar dates. This article adds the time of day and, most importantly, time zones, which turn local date-times into actual moments. Time zones are a human invention with irregular borders and rules that change by political decision, plus daylight saving time, which makes some local times happen twice and others not at all. java.time handles all of this correctly, as long as you pick the right class.
LocalTime and LocalDateTime
LocalTime is a time of day, such as 22, with nanosecond precision and no date or zone. LocalTime.of(22, 30), parse("22:30"), now(), and the constants MIDNIGHT, NOON, MIN and MAX create times. plusHours, plusMinutes and the other plus methods wrap around at midnight, so 22 plus 8 hours is 06. Use isBefore/isAfter to compare, toSecondOfDay() and toNanoOfDay() to convert, and truncatedTo to round down. There is no AM or PM in the data. That's a formatting question (article 70).
LocalDateTime combines a LocalDate and a LocalTime: LocalDateTime.of(2026, 11, 14, 22, 30), date.atTime(7, 15), date.atStartOfDay(). toLocalDate() and toLocalTime() split it again. It describes a wall-clock reading without saying where, so it's not a moment. Use it for things like "the shop opens at 2026-12-24T09" in a context where the zone is implied. For anything that must line up across zones or around daylight saving changes, use ZonedDateTime.
Time zones
The IANA time zone database (also called tzdb or the Olson database) records the rules of every region: its standard offset from UTC, its daylight saving rules, and their complete history. It is updated several times a year as governments change their rules, and the JDK ships a copy that is updated in JDK releases. The tzupdater tool can update an installed JDK separately.
A ZoneId identifies a region by an ID like Europe/Berlin, America/New_York or Asia/Kolkata (about 600 in total, from ZoneId.getAvailableZoneIds()). ZoneId.systemDefault() gives the JVM's zone. Avoid three-letter abbreviations like "EST" or "IST", which are ambiguous (IST is used for India, Ireland and Israel). Offsets are not always whole hours: India is at +05, Nepal at +05, and Lord Howe Island moves by only 30 minutes for summer time.
A ZoneOffset is a fixed amount ahead of or behind UTC, like +05:30 or Z (UTC itself). It is also a ZoneId, one whose rules never change.
ZonedDateTime
A ZonedDateTime is a local date-time plus a zone plus the offset in force at that moment. That makes it a definite instant:
ZonedDateTime launch = ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("America/New_York"));
Instant instant = launch.toInstant(); // 1969-07-16T13:32:00Z
ZonedDateTime inBerlin = launch.withZoneSameInstant(ZoneId.of("Europe/Berlin"));
ZonedDateTime fromInstant = instant.atZone(ZoneId.of("UTC"));
It's created with ZonedDateTime.of(...), localDateTime.atZone(zone), instant.atZone(zone), ZonedDateTime.now(zone) or parse, and toInstant() goes back to machine time. It has the same getters, plus/minus and with methods as LocalDateTime.
Two conversions are easy to confuse:
withZoneSameInstant(zone)keeps the moment and changes the wall-clock reading. Use it for "what time is the New York meeting in Berlin?"withZoneSameLocal(zone)keeps the wall-clock reading and changes the moment. That's rarely what you want.
Daylight saving time: gaps and overlaps
When daylight saving time starts, clocks jump forward and an hour of local times doesn't exist. When it ends, clocks go back and an hour of local times occurs twice. Here's what ZonedDateTime does with them:

- A local time in a gap is moved forward by the length of the gap. 02 on March 29, 2026 in Berlin becomes 03+02.
- A local time in an overlap gets the earlier offset by default (the summer time one).
withLaterOffsetAtOverlap()andwithEarlierOffsetAtOverlap()choose explicitly. If you need to treat gaps and overlaps differently, for example to reject them, askzone.getRules().getValidOffsets(localDateTime): an empty list is a gap, and two entries are an overlap. plusHours(1)adds real elapsed time. One hour after the first 02 in the overlap is the second 02.
ZoneRules, from zone.getRules(), answers the questions behind this: getOffset(instant), getStandardOffset, isDaylightSavings, nextTransition and previousTransition. The last two return a ZoneOffsetTransition with the local times before and after and whether it's a gap or an overlap.
Duration or Period?
On a zoned date-time, time-based amounts (Duration, plusHours, plusMinutes) count real elapsed time, while date-based amounts (Period, plusDays, plusWeeks, plusMonths) keep the local time of day. Around a transition they give different answers. A weekly meeting at 09 plus Duration.ofDays(7) lands at 08 after the clocks go back, because the week had 169 hours. With Period.ofDays(7) or plusWeeks(1), it stays at 09. A calendar day isn't always 24 hours: November 1, 2026 lasts 25 hours in New York.
OffsetDateTime
OffsetDateTime is a local date-time with a fixed offset but no zone rules. It is also an exact instant, but it can't follow daylight saving changes: adding six months to 09+01 gives 09+01 in May, while the same calculation with a Berlin ZonedDateTime correctly gives +02. Use it where only offsets are known or wanted: in data formats and protocols (ISO-8601 timestamps such as 2026-11-14T09:30:00+01:00), and in databases, where JDBC maps TIMESTAMP WITH TIME ZONE to OffsetDateTime (article 65). For calculations involving people's local times, use ZonedDateTime. OffsetTime (a time with an offset) exists too, but is rarely useful.
The program
The demo starts with times that wrap at midnight and combined date-times. It converts the Apollo 11 launch between zones and instants, and shows one instant in zones with half-hour, three-quarter-hour and +14 offsets. A 10 New York meeting is shown in Berlin, Kolkata and Tokyo, and contrasted with withZoneSameLocal. The Berlin part covers the 2026 gap and overlap, the next transition, and the valid offsets of two local times. Then a stand-up meeting is moved forward with a Duration and with a Period across the end of US summer time, and the 25-hour day is measured. Finally the demo compares OffsetDateTime with ZonedDateTime.
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.zone.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
System.out.println("--- LocalTime and LocalDateTime ---");
LocalTime bedtime = LocalTime.of(22, 30);
System.out.println("bedtime " + bedtime + ", wake up " + bedtime.plusHours(8) + " (wraps at midnight)"
+ ", noon is after: " + LocalTime.NOON.isAfter(bedtime.plusHours(8)));
System.out.println("second of day " + bedtime.toSecondOfDay() + ", truncated " + LocalTime.of(9, 41, 27, 500).truncatedTo(ChronoUnit.MINUTES)
+ ", MIDNIGHT " + LocalTime.MIDNIGHT + ", MAX " + LocalTime.MAX);
LocalDateTime departure = LocalDateTime.of(2026, 11, 14, 22, 30);
System.out.println("departure " + departure + ", +3h " + departure.plusHours(3) + ", date part " + departure.toLocalDate()
+ ", combined " + LocalDate.of(2026, 11, 15).atTime(7, 15));
System.out.println("--- zones and instants ---");
ZoneId berlin = ZoneId.of("Europe/Berlin"), newYork = ZoneId.of("America/New_York"), kolkata = ZoneId.of("Asia/Kolkata");
System.out.println("known zone IDs include Europe/Berlin: " + ZoneId.getAvailableZoneIds().contains("Europe/Berlin"));
ZonedDateTime apollo = ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, newYork);
System.out.println("Apollo 11 launch " + apollo + " = " + apollo.toInstant() + " = " + apollo.withZoneSameInstant(berlin));
Instant i = Instant.parse("2026-11-14T12:00:00Z");
for (String zone : List.of("UTC", "Europe/London", "Asia/Kolkata", "Asia/Kathmandu", "Australia/Lord_Howe", "Pacific/Kiritimati"))
System.out.printf(" %-20s %s%n", zone, i.atZone(ZoneId.of(zone)).toLocalDateTime() + " offset " + i.atZone(ZoneId.of(zone)).getOffset());
System.out.println("--- a meeting at 10:00 New York time ---");
ZonedDateTime meeting = ZonedDateTime.of(LocalDate.of(2026, 11, 16), LocalTime.of(10, 0), newYork);
for (ZoneId z : List.of(berlin, kolkata, ZoneId.of("Asia/Tokyo")))
System.out.println(" " + z + ": " + meeting.withZoneSameInstant(z).toLocalDateTime());
System.out.println(" same local time, other zone (different instant!): " + meeting.withZoneSameLocal(berlin));
System.out.println("--- daylight saving time in Berlin, 2026 ---");
ZonedDateTime gap = ZonedDateTime.of(LocalDate.of(2026, 3, 29), LocalTime.of(2, 30), berlin);
System.out.println("02:30 on March 29 doesn't exist, becomes " + gap);
ZonedDateTime overlap = ZonedDateTime.of(LocalDate.of(2026, 10, 25), LocalTime.of(2, 30), berlin);
System.out.println("02:30 on October 25 happens twice: " + overlap + " and " + overlap.withLaterOffsetAtOverlap());
System.out.println("one hour after the first 02:30: " + overlap.plusHours(1));
ZoneRules rules = berlin.getRules();
ZoneOffsetTransition next = rules.nextTransition(Instant.parse("2026-06-01T00:00:00Z"));
System.out.println("next transition after June 1: " + next.getDateTimeBefore() + " -> " + next.getDateTimeAfter()
+ " (" + (next.isGap() ? "gap" : "overlap") + ", " + next.getDuration() + ")");
System.out.println("summer time on July 1: " + rules.isDaylightSavings(Instant.parse("2026-07-01T12:00:00Z"))
+ ", standard offset " + rules.getStandardOffset(Instant.parse("2026-07-01T12:00:00Z")));
LocalDateTime local = LocalDateTime.of(2026, 10, 25, 2, 30);
System.out.println("valid offsets for " + local + ": " + rules.getValidOffsets(local)
+ ", for 2026-03-29T02:30: " + rules.getValidOffsets(LocalDateTime.of(2026, 3, 29, 2, 30)));
System.out.println("--- Duration versus Period across a DST change ---");
ZonedDateTime standup = ZonedDateTime.of(LocalDate.of(2026, 10, 30), LocalTime.of(9, 0), newYork);
System.out.println("stand-up " + standup);
System.out.println("+ Duration 7d " + standup.plus(Duration.ofDays(7)) + " <- wrong local time");
System.out.println("+ Period 7d " + standup.plus(Period.ofDays(7)));
System.out.println("+ plusWeeks(1) " + standup.plusWeeks(1));
ZonedDateTime start = LocalDate.of(2026, 11, 1).atStartOfDay(newYork), end = LocalDate.of(2026, 11, 2).atStartOfDay(newYork);
System.out.println("length of November 1, 2026 in New York: " + Duration.between(start, end));
System.out.println("--- offsets ---");
OffsetDateTime odt = OffsetDateTime.of(2026, 11, 14, 9, 30, 0, 0, ZoneOffset.ofHours(1));
System.out.println(odt + " at UTC " + odt.withOffsetSameInstant(ZoneOffset.UTC) + ", as zoned " + odt.atZoneSameInstant(berlin)
+ ", plus 6 months keeps +01:00: " + odt.plusMonths(6));
System.out.println("zoned, plus 6 months: " + odt.atZoneSameInstant(berlin).plusMonths(6) + " (offset follows the rules)");
System.out.println("ZoneId.of(\"+05:30\") is a " + ZoneId.of("+05:30").getClass().getSimpleName() + ", normalized " + ZoneId.of("UTC").normalized());
}
}
--- LocalTime and LocalDateTime ---
bedtime 22:30, wake up 06:30 (wraps at midnight), noon is after: true
second of day 81000, truncated 09:41, MIDNIGHT 00:00, MAX 23:59:59.999999999
departure 2026-11-14T22:30, +3h 2026-11-15T01:30, date part 2026-11-14, combined 2026-11-15T07:15
--- zones and instants ---
known zone IDs include Europe/Berlin: true
Apollo 11 launch 1969-07-16T09:32-04:00[America/New_York] = 1969-07-16T13:32:00Z = 1969-07-16T14:32+01:00[Europe/Berlin]
UTC 2026-11-14T12:00 offset Z
Europe/London 2026-11-14T12:00 offset Z
Asia/Kolkata 2026-11-14T17:30 offset +05:30
Asia/Kathmandu 2026-11-14T17:45 offset +05:45
Australia/Lord_Howe 2026-11-14T23:00 offset +11:00
Pacific/Kiritimati 2026-11-15T02:00 offset +14:00
--- a meeting at 10:00 New York time ---
Europe/Berlin: 2026-11-16T16:00
Asia/Kolkata: 2026-11-16T20:30
Asia/Tokyo: 2026-11-17T00:00
same local time, other zone (different instant!): 2026-11-16T10:00+01:00[Europe/Berlin]
--- daylight saving time in Berlin, 2026 ---
02:30 on March 29 doesn't exist, becomes 2026-03-29T03:30+02:00[Europe/Berlin]
02:30 on October 25 happens twice: 2026-10-25T02:30+02:00[Europe/Berlin] and 2026-10-25T02:30+01:00[Europe/Berlin]
one hour after the first 02:30: 2026-10-25T02:30+01:00[Europe/Berlin]
next transition after June 1: 2026-10-25T03:00 -> 2026-10-25T02:00 (overlap, PT-1H)
summer time on July 1: true, standard offset +01:00
valid offsets for 2026-10-25T02:30: [+02:00, +01:00], for 2026-03-29T02:30: []
--- Duration versus Period across a DST change ---
stand-up 2026-10-30T09:00-04:00[America/New_York]
+ Duration 7d 2026-11-06T08:00-05:00[America/New_York] <- wrong local time
+ Period 7d 2026-11-06T09:00-05:00[America/New_York]
+ plusWeeks(1) 2026-11-06T09:00-05:00[America/New_York]
length of November 1, 2026 in New York: PT25H
--- offsets ---
2026-11-14T09:30+01:00 at UTC 2026-11-14T08:30Z, as zoned 2026-11-14T09:30+01:00[Europe/Berlin], plus 6 months keeps +01:00: 2027-05-14T09:30+01:00
zoned, plus 6 months: 2027-05-14T09:30+02:00[Europe/Berlin] (offset follows the rules)
ZoneId.of("+05:30") is a ZoneOffset, normalized Z
Results for future dates depend on the time zone data in your JDK. If a government changes its rules, an updated JDK will compute different offsets for those dates. That's another reason to store future appointments as local date-time plus zone.
Common questions
Which class should I use for a timestamp in a REST API?
Instant or OffsetDateTime, serialized as ISO-8601 (2026-11-14T09:30:00Z or with an offset). Avoid LocalDateTime for moments; without a zone, the receiver can't tell when it happened.
How do I convert a time to another time zone?
zoned.withZoneSameInstant(ZoneId.of("Asia/Tokyo")) keeps the moment and changes the local time. withZoneSameLocal keeps the wall-clock reading, which is a different moment, and is rarely what you want.
Why does the server show the wrong local time?
ZoneId.systemDefault() comes from the operating system or the user.timezone property, and servers usually run in UTC. Pass the user's zone explicitly instead of relying on the default, and set -Duser.timezone=UTC on servers to make the default predictable.
How do I keep the JDK's time zone rules up to date?
Update the JDK regularly; each update release contains the latest IANA data. JDK 25.0.5 has tzdata 2026e, for example, which you can see with ZoneRulesProvider.getVersions("UTC"). Between JDK updates, the tzupdater tool from Oracle updates an installed JDK.
Is ZonedDateTime.equals the same as "same moment"?
No. equals also compares the local date-time and the zone, so the same instant in Berlin and in Tokyo is not equal. Use isEqual or compare toInstant() for "same moment".
How do I schedule something "every day at 9 local time"?
Compute each run as LocalDate plus LocalTime.of(9, 0) at the zone, not by adding 24 hours. ZonedDateTime.of(date, time, zone) resolves gaps and overlaps for you. A ScheduledExecutorService with a fixed 24-hour rate drifts by an hour at each DST change.
API reference
| API | Purpose |
|---|---|
LocalTime.of, parse, now, MIDNIGHT, NOON, plusHours, toSecondOfDay, truncatedTo | times of day |
LocalDateTime.of, date.atTime, atStartOfDay, toLocalDate, toLocalTime | date-times without a zone |
ZoneId.of, systemDefault, getAvailableZoneIds, getRules, ZoneOffset.of, ZoneOffset.UTC | zones and offsets |
ZonedDateTime.of, now, parse, localDateTime.atZone, instant.atZone, toInstant | zoned date-times |
withZoneSameInstant, withZoneSameLocal | change zone |
withEarlierOffsetAtOverlap, withLaterOffsetAtOverlap | choose an offset in an overlap |
ZoneRules.getOffset, getValidOffsets, isDaylightSavings, getStandardOffset, nextTransition, previousTransition | zone rules |
ZoneOffsetTransition.isGap, isOverlap, getDateTimeBefore, getDateTimeAfter, getDuration | transitions |
OffsetDateTime.of, withOffsetSameInstant, atZoneSameInstant, OffsetTime | fixed-offset date-times |
Summary
LocalTimeandLocalDateTimeare wall-clock readings without a zone. They're right for schedules in an implied zone, but they aren't moments.ZoneIdnames a region with rules from the IANA database. Use region IDs, not abbreviations.ZoneOffsetis a fixed offset.ZonedDateTimeis a definite moment with a zone. Convert withwithZoneSameInstant,toInstantandatZone.- Daylight saving gaps move local times forward, and overlaps take the earlier offset unless you choose otherwise.
ZoneRulesreveals the details. - Date-based amounts (
Period,plusDays) keep the local time, and time-based ones (Duration,plusHours) count elapsed time.OffsetDateTimesuits protocols and storage,ZonedDateTimehuman calculations.
Practice Questions
A user in Berlin schedules a call for 10 New York time on March 20, 2026. What time is it in Berlin, and how do you compute it?
ZonedDateTime.of(LocalDate.of(2026, 3, 20), LocalTime.of(10, 0), ZoneId.of("America/New_York")).withZoneSameInstant(ZoneId.of("Europe/Berlin")) gives 15. The usual difference is six hours, but the US switches to summer time on March 8 and Europe only on March 29, 2026, so for those three weeks it's five. Hard-coding offsets gets this wrong, while zone rules get it right.
What happens with ZonedDateTime.of(LocalDate.of(2026, 3, 29), LocalTime.of(2, 30), ZoneId.of("Europe/Berlin")), and how can you detect that case?
02 falls in the spring gap, so the result is moved forward to 03+02. To detect it, check zone.getRules().getValidOffsets(localDateTime).isEmpty(), or compare the result's toLocalDateTime() with the requested one.
Why does meeting.plus(Duration.ofDays(7)) sometimes change the meeting's local time, and what should you use instead?
A Duration of 7 days is exactly 168 hours of elapsed time. If a daylight saving change happens during the week, 168 hours later is an hour earlier or later on the wall clock. meeting.plusWeeks(1) or plus(Period.ofDays(7)) adds calendar days and keeps the local time of day.
When is OffsetDateTime the better choice than ZonedDateTime?
When the data only has an offset, or the offset is all you should keep: ISO-8601 timestamps in APIs and files, database TIMESTAMP WITH TIME ZONE values, and log records. It represents an exact instant without depending on zone rules. For calculations in people's local time, such as adding months or scheduling recurring events, use ZonedDateTime, which follows daylight saving rules.
Why shouldn't you store a future appointment as an Instant?
The instant is computed from today's zone rules. If the region changes its rules before the appointment, for example by abolishing daylight saving time, the stored instant would correspond to the wrong local time. Store the LocalDateTime and the ZoneId, and convert to an instant only when needed, using the rules current at that time.
Previous: LocalDate, Period and Temporal Adjusters in Java
Next: Formatting and Parsing Dates in Java, and Converting Legacy Date and Calendar
Related reading
- LocalDate, Period and Temporal Adjusters in Java
- Instant and Duration in Java: Points and Spans on the Time Line
- Connection Pools, DataSources and JPA: JDBC in Real Applications
- JDBC Transactions: Commit, Rollback, Isolation, Save Points and Batch Updates
- JDBC Metadata: DatabaseMetaData, ResultSetMetaData and ParameterMetaData

More in Core Java II
FFM Upcalls, Variadic Functions and errno
Advanced FFM calls: passing Java code to C as function pointers with upcall stubs, binding objects and lambdas, calling variadic functions such as printf, capturing errno and GetLastError, and a C library that calls back into Java. The final article of the course.
Calling Foreign Functions: Linker, Symbol Lookup, Downcalls and jextract
Calling C functions from pure Java with the FFM API: the native linker, default, library and loader symbol lookups, function descriptors, downcall handles, invokeExact, pointers and NULL, struct results, critical calls, enabling native access, and generating bindings with jextract.
FFM Memory Layouts: Structs, Unions, Sequences and Var Handles
Describing C data with FFM memory layouts: value layouts and C types, struct and union layouts, alignment and padding, sequences, layout paths, var handles with base offsets, offset and slice handles, pointers with target layouts, and atomic access modes.
Foreign Function and Memory API: Arenas and Memory Segments
Getting started with the FFM API (final since Java 22): a first foreign call, confined, shared, automatic and global arenas, native and heap memory segments, reading and writing values, C strings and arrays, alignment and byte order, slices, copies, allocators, and memory-mapped files.
Discussion
Sign in to ask a question or share what you learned.