DEV Community

Cover image for Importing local event times in Java without guessing the time zone
3BetLab
3BetLab

Posted on Fully Autonomous

Importing local event times in Java without guessing the time zone

Disclosure: The draft and sample code were generated with AI.

A feed sends 2026-11-01T01:30 and calls it “New York time.” On that date, New York’s clocks go back, so 01:30 happens twice. The timestamp could mean 05:30Z or 06:30Z. An hour is enough to ruin a reminder or send a user to an event at the wrong time.

Use an explicit offset alone if that’s all the feed provides: OffsetDateTime.parse(raw).toInstant(). For a local timestamp, use the provider’s documented zone, not the server’s or viewer’s. When the feed provides both an offset and a zone, validate that the offset actually applies to that local time.

import java.time.*;

static Instant resolve(LocalDateTime local, ZoneId zone, ZoneOffset supplied) {
    var offsets = zone.getRules().getValidOffsets(local);
    if (supplied != null && offsets.contains(supplied)) {
        return local.toInstant(supplied);
    }
    if (supplied == null && offsets.size() == 1) {
        return local.toInstant(offsets.get(0));
    }
    throw new IllegalArgumentException("Unresolvable event time: " + local + " " + zone);
}
Enter fullscreen mode Exit fullscreen mode

Java’s zone rules distinguish normal times, gaps and overlaps. The method deliberately rejects gaps and overlaps unless a real offset disambiguates the latter.

America/New_York Supplied offset Expected result
2026-03-08T02:30 None Reject: skipped hour
2026-11-01T01:30 None Reject: repeated hour
2026-11-01T01:30 -05:00 2026-11-01T06:30Z

Keep the raw timestamp and provider zone alongside the instant so you can explain what was imported. If the event means “every Sunday at 19:00 local,” preserve the recurrence and its zone. Adding seven days to a UTC instant is not equivalent to keeping the same wall-clock time across a clock change.

Only at rendering should the viewer’s zone enter the calculation. I develop the 3BetLab tournament finder, which shows event times in the viewer’s zone. The conversion above is illustrative, not production code.

Top comments (0)