Course contentsShow
← Java
Lesson 1869 of 2,87038. Date and Time API (java.time)Free lesson

ZoneOffset: Representing UTC Offsets

Working with ZoneOffset to represent fixed offsets from UTC, using ofHours(), ofHoursMinutes(), and predefined constants like UTC.

ZoneOffset: Representing UTC Offsets

You'll learn how to work with ZoneOffset to represent fixed offsets from UTC using factory methods and predefined constants.

What Is ZoneOffset?

ZoneOffset represents a fixed time offset from UTC (Coordinated Universal Time). Think of it as the "+05:30" in "2024-01-15T14:30:00+05:30" — it tells you how many hours and minutes ahead or behind UTC a particular time is. Unlike ZoneId, which handles region-based zones with daylight saving rules, ZoneOffset is purely numeric and never changes.

Creating ZoneOffset Instances

The ZoneOffset class provides several factory methods for creating offsets:

ZoneOffset utc = ZoneOffset.UTC;  // The constant for +00:00

ZoneOffset plusFive = ZoneOffset.ofHours(5);  // +05:00
ZoneOffset minusEight = ZoneOffset.ofHours(-8);  // -08:00

ZoneOffset india = ZoneOffset.ofHoursMinutes(5, 30);  // +05:30
ZoneOffset nepal = ZoneOffset.ofHoursMinutes(5, 45);  // +05:45

The UTC constant is predefined for convenience since UTC (+00:00) is commonly used as a reference point.

Working with OffsetDateTime

You typically use ZoneOffset when creating OffsetDateTime instances:

LocalDateTime ldt = LocalDateTime.of(2024, 1, 15, 14, 30);
ZoneOffset offset = ZoneOffset.ofHoursMinutes(5, 30);
OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
// 2024-01-15T14:30+05:30

This is useful when you need to store times with known offsets but don't need full time zone information with DST rules.

Key Takeaway: ZoneOffset represents fixed UTC offsets using ofHours(), ofHoursMinutes(), or the predefined UTC constant — perfect for simple offset calculations without time zone complexity.