Java : ZoneOffset with Examples
ZoneOffset (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ZoneOffset methods.
Summary
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
System.out.println(ZoneOffset.MAX); // +18:00
static final ZoneOffset MIN
System.out.println(ZoneOffset.MIN); // -18:00
static final ZoneOffset UTC
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
System.out.println(offset.getTotalSeconds()); // 0
Fields declared in ZoneId
Methods
Temporal adjustInto (Temporal temporal)
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)
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)
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)
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)
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 ()
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)
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 ()
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
System.out.println(offset.range(ChronoField.OFFSET_SECONDS)); // -64800 - 64800
String toString ()
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
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId