java
35 lines · 6 steps
Converting timestamps across time zones in Java
A utility class that treats epoch millis as an absolute instant and projects it into any zone without shifting the underlying moment.
Explained by
highlit
1import java.time.Instant;
2import java.time.ZoneId;
3import java.time.ZonedDateTime;
4import java.time.format.DateTimeFormatter;
5
6public final class TimeZoneConverter {
7
8 private static final DateTimeFormatter ISO = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
9
10 public static ZonedDateTime fromEpochMillis(long epochMillis, ZoneId zone) {
11 return Instant.ofEpochMilli(epochMillis).atZone(zone);
12 }
13
14 public static long toEpochMillis(ZonedDateTime dateTime) {
15 return dateTime.toInstant().toEpochMilli();
16 }
17
18 public static ZonedDateTime convertZone(ZonedDateTime source, ZoneId target) {
19 return source.withZoneSameInstant(target);
20 }
21
22 public static String describe(long epochMillis, ZoneId zone) {
23 ZonedDateTime local = fromEpochMillis(epochMillis, zone);
24 return String.format("%s (%s) offset=%s",
25 local.format(ISO),
26 zone.getId(),
27 local.getOffset());
28 }
29
30 public static long shiftAcrossZones(long epochMillis, ZoneId from, ZoneId to) {
31 ZonedDateTime original = fromEpochMillis(epochMillis, from);
32 ZonedDateTime shifted = convertZone(original, to);
33 return toEpochMillis(shifted);
34 }
35}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1An epoch instant is absolute; a ZoneId only changes how that same moment is displayed.
- 2java.time types are immutable, so conversions return new objects rather than mutating in place.
- 3withZoneSameInstant reframes a time without altering the point on the timeline it represents.
Related explainers
java
public final class ShippingAddress { private final String recipient; private final String line1; private final String line2;
Building immutable objects with the Builder pattern
builder-pattern
immutability
validation
Intermediate
7 steps
java
package com.example.payments.config; import com.stripe.StripeClient; import org.springframework.beans.factory.annotation.Value;
Swapping payment gateways by profile in Spring
dependency-injection
profiles
configuration
Intermediate
6 steps
java
public class EventBus { private final Map<Class<?>, List<Subscriber>> subscribers = new ConcurrentHashMap<>(); private final Executor executor;
Building an annotation-driven EventBus in Java
publish-subscribe
reflection
annotations
Intermediate
7 steps
typescript
import { Injectable, Scope, Inject } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express';
Request-scoped context service in NestJS
dependency-injection
request-scope
immutability
Intermediate
8 steps
java
public class OrderSerializer { private final ObjectMapper mapper;
Configuring a reusable Jackson ObjectMapper
serialization
json
builder-pattern
Intermediate
8 steps
java
package com.example.maintenance; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
How a scheduled cleanup job runs in Spring
scheduling
cron
transactions
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/converting-timestamps-across-time-zones-in-java-explained-java-c3a2/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.