Java : ZoneOffset with Examples

ZoneOffset (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ZoneOffset methods.


Summary

A time-zone offset from Greenwich/UTC, such as +02:00.

Class diagram

final var offset = ZoneOffset.ofHours(-8);
System.out.println(offset); // -08:00

final var dateTime = OffsetDateTime.of(
        LocalDate.of(2100, 2, 3),
        LocalTime.of(14, 30),
        offset);

System.out.println(dateTime); // 2100-02-03T14:30-08:00

Fields

static final ZoneOffset MAX

Constant for the maximum supported offset.

System.out.println(ZoneOffset.MAX); // +18:00

static final ZoneOffset MIN

Constant for the minimum supported offset.

System.out.println(ZoneOffset.MIN); // -18:00

static final ZoneOffset UTC

The time-zone offset for UTC, with an ID of 'Z'.

final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
System.out.println(offset.getTotalSeconds()); // 0

Fields declared in ZoneId

SHORT_IDS

Please see the link below.

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have the same offset as this object.

final var temporal = OffsetDateTime.of(2100, 4, 7, 0, 0, 0, 0, ZoneOffset.ofHours(3));
System.out.println(temporal); // 2100-04-07T00:00+03:00

final var offset = ZoneOffset.ofHours(-8);
System.out.println(offset); // -08:00

final var ret = offset.adjustInto(temporal);
System.out.println(ret); // 2100-04-07T00:00-08:00

int compareTo (ZoneOffset other)

Compares this offset to another offset in descending order.

final var offset1 = ZoneOffset.ofHours(0);
System.out.println(offset1); // Z

final var offset2 = ZoneOffset.UTC;
System.out.println(offset2); // Z

System.out.println(offset1.compareTo(offset2)); // 0
final var offset1 = ZoneOffset.ofHours(1);
System.out.println(offset1); // +01:00

final var offset2 = ZoneOffset.ofHours(2);
System.out.println(offset2); // +02:00

System.out.println(offset1.compareTo(offset2)); // 3600
final var offset1 = ZoneOffset.ofHoursMinutes(9, 30);
System.out.println(offset1); // +09:30

final var offset2 = ZoneOffset.ofHoursMinutes(-9, -30);
System.out.println(offset2); // -09:30

System.out.println(offset1.compareTo(offset2)); // -68400
final var offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset1); // +01:02:03

final var offset2 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset2); // +01:02:03

System.out.println(offset1.compareTo(offset2)); // 0

boolean equals (Object obj)

Checks if this offset is equal to another offset.

final var offset1 = ZoneOffset.ofHours(0);
System.out.println(offset1); // Z

final var offset2 = ZoneOffset.UTC;
System.out.println(offset2); // Z

System.out.println(offset1.equals(offset2)); // true
final var offset1 = ZoneOffset.ofHours(1);
System.out.println(offset1); // +01:00

final var offset2 = ZoneOffset.ofHours(2);
System.out.println(offset2); // +02:00

System.out.println(offset1.equals(offset2)); // false
final var offset1 = ZoneOffset.ofHoursMinutes(9, 30);
System.out.println(offset1); // +09:30

final var offset2 = ZoneOffset.ofHoursMinutes(-9, -30);
System.out.println(offset2); // -09:30

System.out.println(offset1.equals(offset2)); // false
final var offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset1); // +01:02:03

final var offset2 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset2); // +01:02:03

System.out.println(offset1.equals(offset2)); // true

static ZoneOffset from (TemporalAccessor temporal)

Obtains an instance of ZoneOffset from a temporal object.

final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00

final var temporal = OffsetDateTime.of(2100, 4, 7, 0, 0, 0, 0, offset);
System.out.println(temporal); // 2100-04-07T00:00+09:00

final var ret = ZoneOffset.from(temporal);
System.out.println(ret); // +09:00
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1

final var temporal = ZonedDateTime.of(2100, 4, 7, 12, 30, 45, 0, zone);
System.out.println(temporal); // 2100-04-07T12:30:45-01:00[Etc/GMT+1]

final var ret = ZoneOffset.from(temporal);
System.out.println(ret); // -01:00

int get (TemporalField field)

Gets the value of the specified field from this offset as an int.

final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00

System.out.println(offset.isSupported(ChronoField.OFFSET_SECONDS)); // true
System.out.println(offset.get(ChronoField.OFFSET_SECONDS)); // 3600

String getId ()

Gets the normalized zone offset ID.

final var offset = ZoneOffset.UTC;
System.out.println(offset.getId()); // Z
final var offset = ZoneOffset.ofHours(-9);
System.out.println(offset.getId()); // -09:00
final var offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset.getId()); // +01:02:03

long getLong (TemporalField field)

Gets the value of the specified field from this offset as a long.

final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00

System.out.println(offset.isSupported(ChronoField.OFFSET_SECONDS)); // true
System.out.println(offset.getLong(ChronoField.OFFSET_SECONDS)); // 3600

ZoneRules getRules ()

Gets the associated time-zone rules.

final var roles = ZoneOffset.UTC.getRules();
System.out.println(roles); // ZoneRules[currentStandardOffset=Z]
final var roles = ZoneOffset.ofHours(2).getRules();
System.out.println(roles); // ZoneRules[currentStandardOffset=+02:00]
final var roles = ZoneOffset.ofHoursMinutes(-9, -30).getRules();
System.out.println(roles); // ZoneRules[currentStandardOffset=-09:30]
final var roles = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3).getRules();
System.out.println(roles); // ZoneRules[currentStandardOffset=+01:02:03]

int getTotalSeconds ()

Gets the total zone offset in seconds.

final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
System.out.println(offset.getTotalSeconds()); // 0
final var offset = ZoneOffset.ofHours(2);
System.out.println(offset); // +02:00
System.out.println(offset.getTotalSeconds()); // 7200
final var offset = ZoneOffset.ofHoursMinutes(-9, -30);
System.out.println(offset); // -09:30
System.out.println(offset.getTotalSeconds()); // -34200
final var offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset); // +01:02:03
System.out.println(offset.getTotalSeconds()); // 3723

int hashCode ()

A hash code for this offset.

final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
System.out.println(offset.hashCode()); // 0
final var offset = ZoneOffset.ofHours(2);
System.out.println(offset); // +02:00
System.out.println(offset.hashCode()); // 7200
final var offset = ZoneOffset.ofHoursMinutes(-9, -30);
System.out.println(offset); // -09:30
System.out.println(offset.hashCode()); // -34200
final var offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
System.out.println(offset); // +01:02:03
System.out.println(offset.hashCode()); // 3723

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00

System.out.println("-- supported fields --");
for (final var field : ChronoField.values()) {
    if (offset.isSupported(field)) {
        System.out.println(field);
    }
}

// Result
// ↓
//-- supported fields --
//OffsetSeconds

static ZoneOffset of (String offsetId)

Obtains an instance of ZoneOffset using the ID.

System.out.println(ZoneOffset.of("Z")); // Z
System.out.println(ZoneOffset.of("+09")); // +09:00
System.out.println(ZoneOffset.of("-08")); // -08:00
System.out.println(ZoneOffset.of("+01:02")); // +01:02
System.out.println(ZoneOffset.of("-01:02:03")); // -01:02:03

static ZoneOffset ofHours (int hours)

Obtains an instance of ZoneOffset using an offset in hours.

System.out.println(ZoneOffset.ofHours(0)); // Z
System.out.println(ZoneOffset.ofHours(1)); // +01:00
System.out.println(ZoneOffset.ofHours(2)); // +02:00

System.out.println(ZoneOffset.ofHours(-1)); // -01:00
System.out.println(ZoneOffset.ofHours(-2)); // -02:00

System.out.println(ZoneOffset.ofHours(18)); // +18:00
System.out.println(ZoneOffset.ofHours(-18)); // -18:00
try {
    final var ret = ZoneOffset.ofHours(19);
} catch (DateTimeException e) {
    System.out.println("DateTimeException! : " + e.getMessage());
}

// Result
// ↓
//DateTimeException! : Zone offset hours not in valid range:
// value 19 is not in the range -18 to 18

static ZoneOffset ofHoursMinutes (int hours, int minutes)

Obtains an instance of ZoneOffset using an offset in hours and minutes.

System.out.println(ZoneOffset.ofHoursMinutes(0, 0)); // Z
System.out.println(ZoneOffset.ofHoursMinutes(1, 10)); // +01:10
System.out.println(ZoneOffset.ofHoursMinutes(2, 25)); // +02:25

System.out.println(ZoneOffset.ofHoursMinutes(-3, -30)); // -03:30
System.out.println(ZoneOffset.ofHoursMinutes(-4, -45)); // -04:45

static ZoneOffset ofHoursMinutesSeconds (int hours, int minutes, int seconds)

Obtains an instance of ZoneOffset using an offset in hours, minutes and seconds.

System.out.println(ZoneOffset.ofHoursMinutesSeconds(0, 0, 0)); // Z
System.out.println(ZoneOffset.ofHoursMinutesSeconds(1, 10, 12)); // +01:10:12
System.out.println(ZoneOffset.ofHoursMinutesSeconds(2, 25, 14)); // +02:25:14

System.out.println(ZoneOffset.ofHoursMinutesSeconds(-3, -30, 0)); // -03:30
System.out.println(ZoneOffset.ofHoursMinutesSeconds(-4, 0, -55)); // -04:00:55

static ZoneOffset ofTotalSeconds (int totalSeconds)

Obtains an instance of ZoneOffset specifying the total offset in seconds

System.out.println(ZoneOffset.ofTotalSeconds(0)); // Z
System.out.println(ZoneOffset.ofTotalSeconds(60)); // +00:01
System.out.println(ZoneOffset.ofTotalSeconds(3600)); // +01:00

System.out.println(ZoneOffset.ofTotalSeconds(-2)); // -00:00:02
System.out.println(ZoneOffset.ofTotalSeconds(-3723)); // -01:02:03

<R> R query (TemporalQuery<R> query)

Queries this offset using the specified query.

final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
System.out.println(offset.query(TemporalQueries.offset())); // +01:00

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
System.out.println(offset.range(ChronoField.OFFSET_SECONDS)); // -64800 - 64800

String toString ()

Outputs this offset as a String, using the normalized ID.

final var str1 = ZoneOffset.UTC.toString();
System.out.println(str1); // Z

final var str2 = ZoneOffset.ofHours(-9).toString();
System.out.println(str2); // -09:00

final var str3 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3).toString();
System.out.println(str3); // +01:02:03

Methods declared in ZoneId

getAvailableZoneIds, getDisplayName, normalized, of, ofOffset, systemDefault

Please see the link below.


Related posts

To top of page