Sub topic 10
OffsetDateTime for Storage and Data Exchange
In the last chapter you used ZonedDateTime. It knows the full rules of a time zone, such as "America/New_York". Sometimes you don't need those rules. You only need to say exactly when something happened, in a form any other system can read. OffsetDateTime covers that case.
What OffsetDateTime Holds
OffsetDateTime is a date and time plus a fixed offset from UTC. An offset is a plain number of hours and minutes, such as -04:00 or +09:00. UTC is the world's reference time, with offset +00:00, often written Z.
The type has no zone name, so it knows nothing about daylight saving rules. It only says "this local time, shifted by this amount from UTC." That is enough to point to one exact instant.
| Type | Contains | Good for |
|---|---|---|
| `LocalDateTime` | Date and time only | Wall-clock times with no place attached |
| `ZonedDateTime` | Date, time, offset, zone rules | Scheduling and arithmetic in a real place |
| `OffsetDateTime` | Date, time, offset | Databases, JSON, logs, APIs |
Why It Suits Storage
Many databases and data formats store a timestamp with an offset but no zone name. ISO 8601, the standard text format for dates, works this way, for example 2024-07-01T09:00-04:00. An OffsetDateTime maps straight onto that format. Nothing gets lost and nothing needs guessing.
A Worked Example
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class Offsets {
public static void main(String[] args) {
OffsetDateTime order = OffsetDateTime.of(
LocalDateTime.of(2024, 7, 1, 9, 0), ZoneOffset.ofHours(-4));
System.out.println(order); // 2024-07-01T09:00-04:00
System.out.println(order.toInstant()); // 2024-07-01T13:00:00Z
OffsetDateTime utc = order.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(utc); // 2024-07-01T13:00Z
OffsetDateTime parsed = OffsetDateTime.parse("2024-07-01T15:00+02:00");
System.out.println(parsed.isEqual(order)); // true
System.out.println(parsed.equals(order)); // false
ZonedDateTime zoned = ZonedDateTime.of(2024, 7, 1, 9, 0, 0, 0,
ZoneId.of("America/New_York"));
System.out.println(zoned.toOffsetDateTime()); // 2024-07-01T09:00-04:00
}
}Here is what each part does:
withOffsetSameInstantkeeps the same moment and shows it at a new offset. Converting to UTC before you save is a common habit.parsereads ISO text directly. You don't need a formatter.isEqualchecks whether two values are the same instant.equalsalso checks that the local time and offset match, so it returnsfalsehere.
When Not to Use It
An offset is not a time zone. New York is -04:00 in summer and -05:00 in winter. If you add six months to an OffsetDateTime, the offset stays -04:00, which is wrong for a winter date in New York. For arithmetic in a real place, use ZonedDateTime.
Exercise
You have a = OffsetDateTime.parse("2024-05-01T10:00+01:00") and b = OffsetDateTime.parse("2024-05-01T09:00Z"). What do a.equals(b) and a.isEqual(b) return, and why?
Show answer
a.equals(b) returns false because the local times (10:00 and 09:00) and offsets (+01:00 and Z) differ. a.isEqual(b) returns true because both represent the same instant, 09:00 UTC.
Exercise
Why is OffsetDateTime a poor choice for computing "the same local time three months from now" for a user in Europe/Paris?
Show answer
OffsetDateTime keeps a fixed offset and has no daylight saving rules. Three months later Paris may use a different offset, so the result could be an hour off. ZonedDateTime with ZoneId "Europe/Paris" applies the correct rules.