Java : OffsetTime with Examples

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


Summary

A time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00.

Class diagram

final var time = OffsetTime.of(13, 30, 15, 123000000, ZoneOffset.ofHours(4));
System.out.println(time); // 13:30:15.123+04:00

System.out.println(time.getHour()); // 13
System.out.println(time.getMinute()); // 30
System.out.println(time.getSecond()); // 15
System.out.println(time.getNano()); // 123000000
System.out.println(time.getOffset()); // +04:00

Fields

static final OffsetTime MAX

The maximum supported OffsetTime, '23:59:59.999999999-18:00'.

System.out.println(OffsetTime.MAX); // 23:59:59.999999999-18:00
System.out.println(OffsetTime.MAX.getHour()); // 23
System.out.println(OffsetTime.MAX.getMinute()); // 59
System.out.println(OffsetTime.MAX.getSecond()); // 59
System.out.println(OffsetTime.MAX.getNano()); // 999999999
System.out.println(OffsetTime.MAX.getOffset()); // -18:00

static final OffsetTime MIN

The minimum supported OffsetTime, '00:00:00+18:00'.

System.out.println(OffsetTime.MIN); // 00:00+18:00
System.out.println(OffsetTime.MIN.getHour()); // 0
System.out.println(OffsetTime.MIN.getMinute()); // 0
System.out.println(OffsetTime.MIN.getSecond()); // 0
System.out.println(OffsetTime.MIN.getNano()); // 0
System.out.println(OffsetTime.MIN.getOffset()); // +18:00

Methods

Temporal adjustInto (Temporal temporal)

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

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

final var temporal = OffsetDateTime.of(2100, 1, 1, 2, 3, 0, 0, offset);
System.out.println(temporal); // 2100-01-01T02:03Z

final var time = OffsetTime.of(23, 59, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 23:59+04:00

final var ret = time.adjustInto(temporal);
System.out.println(ret); // 2100-01-01T23:59+04:00
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1

final var temporal = ZonedDateTime.of(2100, 1, 1, 2, 3, 0, 0, zone);
System.out.println(temporal); // 2100-01-01T02:03-01:00[Etc/GMT+1]

final var time = OffsetTime.of(20, 30, 40, 123456, ZoneOffset.UTC);
System.out.println(time); // 20:30:40.000123456Z

final var ret = time.adjustInto(temporal);
System.out.println(ret); // 2100-01-01T20:30:40.000123456-01:00[Etc/GMT+1]

OffsetDateTime atDate (LocalDate date)

Combines this time with a date to create an OffsetDateTime.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

final var ret1 = time.atDate(LocalDate.of(2100, 4, 12));
System.out.println(ret1); // 2100-04-12T13:30:15Z

final var ret2 = time.atDate(LocalDate.of(-900, 1, 1));
System.out.println(ret2); // -0900-01-01T13:30:15Z

int compareTo (OffsetTime other)

Compares this OffsetTime to another time.

final var time1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time1); // 00:00+04:00

final var time2 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(-8));
System.out.println(time2); // 00:00-08:00

System.out.println(time1.compareTo(time2)); // -1
final var time1 = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:30:15Z

final var time2 = OffsetTime.of(14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time2); // 14:30:15Z

System.out.println(time1.compareTo(time2)); // -1
final var time1 = OffsetTime.of(13, 31, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:31Z

final var time2 = OffsetTime.of(13, 30, 59, 0, ZoneOffset.UTC);
System.out.println(time2); // 13:30:59Z

System.out.println(time1.compareTo(time2)); // 1
final var time1 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 23:30Z

final var time2 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 23:30Z

System.out.println(time1.compareTo(time2)); // 0

boolean equals (Object obj)

Checks if this time is equal to another time.

final var time1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time1); // 00:00+04:00

final var time2 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(-8));
System.out.println(time2); // 00:00-08:00

System.out.println(time1.equals(time2)); // false
final var time1 = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:30:15Z

final var time2 = OffsetTime.of(14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time2); // 14:30:15Z

System.out.println(time1.equals(time2)); // false
final var time1 = OffsetTime.of(13, 31, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:31Z

final var time2 = OffsetTime.of(13, 30, 59, 0, ZoneOffset.UTC);
System.out.println(time2); // 13:30:59Z

System.out.println(time1.equals(time2)); // false
final var time1 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 23:30Z

final var time2 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 23:30Z

System.out.println(time1.equals(time2)); // true

String format (DateTimeFormatter formatter)

Formats this time using the specified formatter.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 13:30:15+04:00

{
    final var ret1 = time.format(DateTimeFormatter.ISO_OFFSET_TIME);
    System.out.println(ret1); // 13:30:15+04:00

    final var ret2 = time.format(DateTimeFormatter.ofPattern("HH:mm:ss O"));
    System.out.println(ret2); // 13:30:15 GMT+4
}
{
    System.out.println(Locale.getDefault().toLanguageTag()); // en-US

    final var ret1 = time.format(
            DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
    System.out.println(ret1); // 1:30:15 PM

    final var ret2 = time.format(
            DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
    System.out.println(ret2); // 1:30 PM

    try {
        final var ret3 = time.format(
                DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG));
    } catch (DateTimeException e) {
        System.out.println("DateTimeException! : " + e.getMessage());
    }

    // Result
    // ↓
    //DateTimeException! :
    // Unable to extract ZoneId from temporal 13:30:15+04:00 with chronology ISO
}

static OffsetTime from (TemporalAccessor temporal)

Obtains an instance of OffsetTime from a temporal object.

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

final var temporal = OffsetDateTime.of(2100, 12, 15, 4, 5, 6, 123000, offset);
System.out.println(temporal); // 2100-12-15T04:05:06.000123+12:00
System.out.println(OffsetTime.from(temporal)); // 04:05:06.000123+12:00
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1

final var temporal = ZonedDateTime.of(1800, 1, 3, 1, 2, 3, 456, zone);
System.out.println(temporal); // 1800-01-03T01:02:03.000000456-01:00[Etc/GMT+1]
System.out.println(OffsetTime.from(temporal)); // 01:02:03.000000456-01:00

int get (TemporalField field)

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

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.ofHours(1));
System.out.println(time); // 13:30:15.000000123+01:00

System.out.println(time.get(ChronoField.HOUR_OF_DAY)); // 13
System.out.println(time.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(time.get(ChronoField.SECOND_OF_MINUTE)); // 15
System.out.println(time.get(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(time.get(ChronoField.OFFSET_SECONDS)); // 3600

int getHour ()

Gets the hour-of-day field.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getHour()); // 13

long getLong (TemporalField field)

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

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.ofHours(1));
System.out.println(time); // 13:30:15.000000123+01:00

System.out.println(time.getLong(ChronoField.HOUR_OF_DAY)); // 13
System.out.println(time.getLong(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(time.getLong(ChronoField.SECOND_OF_MINUTE)); // 15
System.out.println(time.getLong(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(time.getLong(ChronoField.OFFSET_SECONDS)); // 3600

int getMinute ()

Gets the minute-of-hour field.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getMinute()); // 30

int getNano ()

Gets the nano-of-second field.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getNano()); // 0
final var time = OffsetTime.of(13, 30, 15, 123456789, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.123456789Z
System.out.println(time.getNano()); // 123456789

ZoneOffset getOffset ()

Gets the zone offset, such as '+01:00'.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getOffset()); // Z
final var time = OffsetTime.of(1, 2, 3, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 01:02:03+04:00
System.out.println(time.getOffset()); // +04:00

int getSecond ()

Gets the second-of-minute field.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getSecond()); // 15

int hashCode ()

A hash code for this time.

final var time1 = OffsetTime.of(1, 2, 3, 4, ZoneOffset.UTC);
System.out.println(time1.hashCode()); // -736645786

final var time2 = OffsetTime.of(1, 2, 3, 4, ZoneOffset.ofHours(4));
System.out.println(time2.hashCode()); // -736651994

final var time3 = OffsetTime.of(23, 59, 59, 0, ZoneOffset.UTC);
System.out.println(time3.hashCode()); // 1437890708

boolean isAfter (OffsetTime other)

Checks if the instant of this OffsetTime is after that of the specified time applying both times to a common date.

final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:01Z

final var time2 = OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time4 = OffsetTime.of(12, 2, 0, 0, ZoneOffset.UTC);

System.out.println(time2); // 12:00Z
System.out.println(time3); // 12:01Z
System.out.println(time4); // 12:02Z

System.out.println(time.isAfter(time2)); // true
System.out.println(time.isAfter(time3)); // false
System.out.println(time.isAfter(time4)); // false
final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:01+04:00

final var time2 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(-4));
final var time4 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(8));

System.out.println(time2); // 12:01Z
System.out.println(time3); // 12:01-04:00
System.out.println(time4); // 12:01+08:00

System.out.println(time.isAfter(time2)); // false
System.out.println(time.isAfter(time3)); // false
System.out.println(time.isAfter(time4)); // true

boolean isBefore (OffsetTime other)

Checks if the instant of this OffsetTime is before that of the specified time applying both times to a common date.

final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:01Z

final var time2 = OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time4 = OffsetTime.of(12, 2, 0, 0, ZoneOffset.UTC);

System.out.println(time2); // 12:00Z
System.out.println(time3); // 12:01Z
System.out.println(time4); // 12:02Z

System.out.println(time.isBefore(time2)); // false
System.out.println(time.isBefore(time3)); // false
System.out.println(time.isBefore(time4)); // true
final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:01+04:00

final var time2 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(-4));
final var time4 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(8));

System.out.println(time2); // 12:01Z
System.out.println(time3); // 12:01-04:00
System.out.println(time4); // 12:01+08:00

System.out.println(time.isBefore(time2)); // true
System.out.println(time.isBefore(time3)); // true
System.out.println(time.isBefore(time4)); // false

boolean isEqual (OffsetTime other)

Checks if the instant of this OffsetTime is equal to that of the specified time applying both times to a common date.

final var time1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time1); // 00:00+04:00

final var time2 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(-8));
System.out.println(time2); // 00:00-08:00

System.out.println(time1.isEqual(time2)); // false
final var time1 = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:30:15Z

final var time2 = OffsetTime.of(14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time2); // 14:30:15Z

System.out.println(time1.isEqual(time2)); // false
final var time1 = OffsetTime.of(13, 31, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:31Z

final var time2 = OffsetTime.of(13, 30, 59, 0, ZoneOffset.UTC);
System.out.println(time2); // 13:30:59Z

System.out.println(time1.isEqual(time2)); // false
final var time1 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 23:30Z

final var time2 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 23:30Z

System.out.println(time1.isEqual(time2)); // true

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);

System.out.println(time.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(time.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(time.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(time.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(time.isSupported(ChronoField.OFFSET_SECONDS)); // true

System.out.println(time.isSupported(ChronoField.YEAR)); // false

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);

System.out.println(time.isSupported(ChronoUnit.HOURS)); // true
System.out.println(time.isSupported(ChronoUnit.MINUTES)); // true
System.out.println(time.isSupported(ChronoUnit.SECONDS)); // true
System.out.println(time.isSupported(ChronoUnit.NANOS)); // true

System.out.println(time.isSupported(ChronoUnit.DAYS)); // false

OffsetTime minus (long amountToSubtract, TemporalUnit unit)

Returns a copy of this time with the specified amount subtracted.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z

System.out.println(time.minus(1, ChronoUnit.HOURS)); // 12:30:15.000000123Z
System.out.println(time.minus(2, ChronoUnit.MINUTES)); // 13:28:15.000000123Z
System.out.println(time.minus(3, ChronoUnit.SECONDS)); // 13:30:12.000000123Z
System.out.println(time.minus(4, ChronoUnit.NANOS)); // 13:30:15.000000119Z

OffsetTime minus (TemporalAmount amountToSubtract)

Returns a copy of this time with the specified amount subtracted.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z

System.out.println(time.minus(Duration.ofHours(1))); // 12:30:15.000000123Z
System.out.println(time.minus(Duration.ofMinutes(2))); // 13:28:15.000000123Z
System.out.println(time.minus(Duration.ofSeconds(3))); // 13:30:12.000000123Z
System.out.println(time.minus(Duration.ofNanos(4))); // 13:30:15.000000119Z

OffsetTime minusHours (long hours)

Returns a copy of this OffsetTime with the specified number of hours subtracted.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

System.out.println(time.minusHours(1)); // 12:30:15Z
System.out.println(time.minusHours(2)); // 11:30:15Z
System.out.println(time.minusHours(13)); // 00:30:15Z
System.out.println(time.minusHours(14)); // 23:30:15Z

OffsetTime minusMinutes (long minutes)

Returns a copy of this OffsetTime with the specified number of minutes subtracted.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

System.out.println(time.minusMinutes(1)); // 13:29:15Z
System.out.println(time.minusMinutes(2)); // 13:28:15Z
System.out.println(time.minusMinutes(30)); // 13:00:15Z
System.out.println(time.minusMinutes(31)); // 12:59:15Z
System.out.println(time.minusMinutes(60 * 13)); // 00:30:15Z
System.out.println(time.minusMinutes(60 * 14)); // 23:30:15Z

OffsetTime minusNanos (long nanos)

Returns a copy of this OffsetTime with the specified number of nanoseconds subtracted.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z

System.out.println(time.minusNanos(1L)); // 13:30:15.000000122Z
System.out.println(time.minusNanos(2L)); // 13:30:15.000000121Z
System.out.println(time.minusNanos(123L)); // 13:30:15Z
System.out.println(time.minusNanos(124L)); // 13:30:14.999999999Z

OffsetTime minusSeconds (long seconds)

Returns a copy of this OffsetTime with the specified number of seconds subtracted.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

System.out.println(time.minusSeconds(1)); // 13:30:14Z
System.out.println(time.minusSeconds(2)); // 13:30:13Z
System.out.println(time.minusSeconds(15)); // 13:30Z
System.out.println(time.minusSeconds(16)); // 13:29:59Z
System.out.println(time.minusSeconds(76)); // 13:28:59Z
System.out.println(time.minusSeconds(60 * 30)); // 13:00:15Z
System.out.println(time.minusSeconds(60 * 31)); // 12:59:15Z

static OffsetTime now ()

Obtains the current time from the system clock in the default time-zone.

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

System.out.println(OffsetTime.now()); // 01:05:07.322175800-07:00

static OffsetTime now (Clock clock)

Obtains the current time from the specified clock.

// A clock advanced 5 hours.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(5));

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

final var time1 = OffsetTime.now();
System.out.println(time1); // 01:02:46.655392100-07:00

final var time2 = OffsetTime.now(clock);
System.out.println(time2); // 06:02:46.656392400-07:00

static OffsetTime now (ZoneId zone)

Obtains the current time from the system clock in the specified time-zone.

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

final var zone2 = ZoneId.of("Etc/GMT+1");
System.out.println(zone2); // Etc/GMT+1

final var time1 = OffsetTime.now(zone1);
System.out.println(time1); // 08:06:37.659861400Z

final var time2 = OffsetTime.now(zone2);
System.out.println(time2); // 07:06:37.660860500-01:00

static OffsetTime of (int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)

Obtains an instance of OffsetTime from an hour, minute, second and nanosecond.

final var ret1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(ret1); // 00:00Z

final var ret2 = OffsetTime.of(12, 30, 45, 1234, ZoneOffset.ofHours(4));
System.out.println(ret2); // 12:30:45.000001234+04:00

static OffsetTime of (LocalTime time, ZoneOffset offset)

Obtains an instance of OffsetTime from a local time and an offset.

final var localTime = LocalTime.of(0, 0);
System.out.println(localTime); // 00:00

final var ret = OffsetTime.of(localTime, ZoneOffset.UTC);
System.out.println(ret); // 00:00Z
final var localTime = LocalTime.of(12, 30, 45, 123);
System.out.println(localTime); // 12:30:45.000000123

final var ret = OffsetTime.of(localTime, ZoneOffset.ofHours(4));
System.out.println(ret); // 12:30:45.000000123+04:00

static OffsetTime ofInstant (Instant instant, ZoneId zone)

Obtains an instance of OffsetTime from an Instant and zone ID.

final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

final var ret1 = OffsetTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(ret1); // 00:00Z

final var ret2 = OffsetTime.ofInstant(instant, ZoneOffset.ofHours(8));
System.out.println(ret2); // 08:00+08:00

final var ret3 = OffsetTime.ofInstant(instant, ZoneOffset.ofHours(-8));
System.out.println(ret3); // 16:00-08:00

static OffsetTime parse (CharSequence text)

Obtains an instance of OffsetTime from a text string such as 10:15:30+01:00.

System.out.println(OffsetTime.parse("12:30Z")); // 12:30Z
System.out.println(OffsetTime.parse("23:59:59.123456789Z")); // 23:59:59.123456789Z

System.out.println(OffsetTime.parse("02:05:01+04:00")); // 02:05:01+04:00

static OffsetTime parse (CharSequence text, DateTimeFormatter formatter)

Obtains an instance of OffsetTime from a text string using a specific formatter.

final var ret1 = OffsetTime.parse(
        "12:30:15+04:00", DateTimeFormatter.ISO_OFFSET_TIME);
System.out.println(ret1); // 12:30:15+04:00

final var ret2 = OffsetTime.parse(
        "2:5:1 Z", DateTimeFormatter.ofPattern("H:m:s X"));
System.out.println(ret2); // 02:05:01Z

final var ret3 = OffsetTime.parse(
        "12:30:15 GMT-8", DateTimeFormatter.ofPattern("H:m:s O"));
System.out.println(ret3); // 12:30:15-08:00

OffsetTime plus (long amountToAdd, TemporalUnit unit)

Returns a copy of this time with the specified amount added.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z

System.out.println(time.plus(1, ChronoUnit.HOURS)); // 14:30:15.000000123Z
System.out.println(time.plus(2, ChronoUnit.MINUTES)); // 13:32:15.000000123Z
System.out.println(time.plus(3, ChronoUnit.SECONDS)); // 13:30:18.000000123Z
System.out.println(time.plus(4, ChronoUnit.NANOS)); // 13:30:15.000000127Z

OffsetTime plus (TemporalAmount amountToAdd)

Returns a copy of this time with the specified amount added.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z

System.out.println(time.plus(Duration.ofHours(1))); // 14:30:15.000000123Z
System.out.println(time.plus(Duration.ofMinutes(2))); // 13:32:15.000000123Z
System.out.println(time.plus(Duration.ofSeconds(3))); // 13:30:18.000000123Z
System.out.println(time.plus(Duration.ofNanos(4))); // 13:30:15.000000127Z

OffsetTime plusHours (long hours)

Returns a copy of this OffsetTime with the specified number of hours added.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

System.out.println(time.plusHours(1)); // 14:30:15Z
System.out.println(time.plusHours(2)); // 15:30:15Z
System.out.println(time.plusHours(10)); // 23:30:15Z
System.out.println(time.plusHours(11)); // 00:30:15Z

OffsetTime plusMinutes (long minutes)

Returns a copy of this OffsetTime with the specified number of minutes added.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

System.out.println(time.plusMinutes(1)); // 13:31:15Z
System.out.println(time.plusMinutes(2)); // 13:32:15Z
System.out.println(time.plusMinutes(29)); // 13:59:15Z
System.out.println(time.plusMinutes(30)); // 14:00:15Z
System.out.println(time.plusMinutes(31)); // 14:01:15Z
System.out.println(time.plusMinutes(60 * 10)); // 23:30:15Z
System.out.println(time.plusMinutes(60 * 11)); // 00:30:15Z

OffsetTime plusNanos (long nanos)

Returns a copy of this OffsetTime with the specified number of nanoseconds added.

final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z

System.out.println(time.plusNanos(1L)); // 13:30:15.000000124Z
System.out.println(time.plusNanos(2L)); // 13:30:15.000000125Z
System.out.println(time.plusNanos(999999999L - 123)); // 13:30:15.999999999Z
System.out.println(time.plusNanos(999999999L - 122)); // 13:30:16Z

OffsetTime plusSeconds (long seconds)

Returns a copy of this OffsetTime with the specified number of seconds added.

final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z

System.out.println(time.plusSeconds(1)); // 13:30:16Z
System.out.println(time.plusSeconds(2)); // 13:30:17Z
System.out.println(time.plusSeconds(44)); // 13:30:59Z
System.out.println(time.plusSeconds(45)); // 13:31Z
System.out.println(time.plusSeconds(46)); // 13:31:01Z
System.out.println(time.plusSeconds(60 * 29)); // 13:59:15Z
System.out.println(time.plusSeconds(60 * 30)); // 14:00:15Z

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

Queries this time using the specified query.

final var time = OffsetTime.of(12, 30, 20, 123456789, ZoneOffset.ofHours(4));
System.out.println(time); // 12:30:20.123456789+04:00
System.out.println(time.query(TemporalQueries.localTime())); // 12:30:20.123456789
System.out.println(time.query(TemporalQueries.offset())); // +04:00
System.out.println(time.query(TemporalQueries.precision())); // Nanos

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var time = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(time.range(ChronoField.HOUR_OF_DAY)); // 0 - 23
System.out.println(time.range(ChronoField.MINUTE_OF_HOUR)); // 0 - 59
System.out.println(time.range(ChronoField.SECOND_OF_MINUTE)); // 0 - 59
System.out.println(time.range(ChronoField.NANO_OF_SECOND)); // 0 - 999999999
System.out.println(time.range(ChronoField.OFFSET_SECONDS)); // -64800 - 64800

long toEpochSecond (LocalDate date)

Converts this OffsetTime to the number of seconds since the epoch of 1970-01-01T00:00:00Z.

final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

final long ret = time.toEpochSecond(date);
System.out.println(ret); // 4110870600
System.out.println(Instant.ofEpochSecond(ret)); // 2100-04-08T12:30:00Z
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.ofHours(8));
System.out.println(time); // 12:30+08:00

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

final long ret = time.toEpochSecond(date);
System.out.println(ret); // 4110841800
System.out.println(Instant.ofEpochSecond(ret)); // 2100-04-08T04:30:00Z

LocalTime toLocalTime ()

Gets the LocalTime part of this date-time.

final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z

final var ret = time.toLocalTime();
System.out.println(ret); // 12:30
final var time = OffsetTime.of(1, 2, 3, 456, ZoneOffset.ofHours(8));
System.out.println(time); // 01:02:03.000000456+08:00

final var ret = time.toLocalTime();
System.out.println(ret); // 01:02:03.000000456

String toString ()

Outputs this time as a String, such as 10:15:30+01:00.

final var str1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC).toString();
System.out.println(str1); // 00:00Z

final var str2 = OffsetTime.of(1, 2, 3, 456, ZoneOffset.ofHours(4)).toString();
System.out.println(str2); // 01:02:03.000000456+04:00

OffsetTime truncatedTo (TemporalUnit unit)

Returns a copy of this OffsetTime with the time truncated.

final var time = OffsetTime.of(12, 30, 15, 123456789, ZoneOffset.UTC);
System.out.println(time); // 12:30:15.123456789Z

System.out.println(time.truncatedTo(ChronoUnit.HOURS)); // 12:00Z
System.out.println(time.truncatedTo(ChronoUnit.MINUTES)); // 12:30Z
System.out.println(time.truncatedTo(ChronoUnit.SECONDS)); // 12:30:15Z
System.out.println(time.truncatedTo(ChronoUnit.NANOS)); // 12:30:15.123456789Z

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another time in terms of the specified unit.

final var time1 = OffsetTime.of(1, 2, 0, 0, ZoneOffset.UTC);
final var time2 = OffsetTime.of(3, 32, 15, 123, ZoneOffset.UTC);
System.out.println(time1); // 01:02Z
System.out.println(time2); // 03:32:15.000000123Z

System.out.println(time1.until(time2, ChronoUnit.HOURS)); // 2
System.out.println(time1.until(time2, ChronoUnit.MINUTES)); // 150
System.out.println(time1.until(time2, ChronoUnit.SECONDS)); // 9015
System.out.println(time1.until(time2, ChronoUnit.NANOS)); // 9015000000123
final var time1 = OffsetTime.of(1, 2, 3, 0, ZoneOffset.UTC);
final var time2 = OffsetTime.of(1, 1, 1, 0, ZoneOffset.UTC);
System.out.println(time1); // 01:02:03Z
System.out.println(time2); // 01:01:01Z

System.out.println(time1.until(time2, ChronoUnit.HOURS)); // 0
System.out.println(time1.until(time2, ChronoUnit.MINUTES)); // -1
System.out.println(time1.until(time2, ChronoUnit.SECONDS)); // -62
System.out.println(time1.until(time2, ChronoUnit.NANOS)); // -62000000000
final var time1 = OffsetTime.of(1, 1, 1, 0, ZoneOffset.ofHours(3));
final var time2 = OffsetTime.of(1, 1, 1, 0, ZoneOffset.ofHours(-5));
System.out.println(time1); // 01:01:01+03:00
System.out.println(time2); // 01:01:01-05:00

System.out.println(time1.until(time2, ChronoUnit.HOURS)); // 8
System.out.println(time1.until(time2, ChronoUnit.MINUTES)); // 480
System.out.println(time1.until(time2, ChronoUnit.SECONDS)); // 28800
System.out.println(time1.until(time2, ChronoUnit.NANOS)); // 28800000000000

OffsetTime with (TemporalAdjuster adjuster)

Returns an adjusted copy of this time.

final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z

final var adjuster = new TemporalAdjuster() {
    @Override
    public Temporal adjustInto(Temporal temporal) {
        return temporal.with(ChronoField.SECOND_OF_MINUTE, 59);
    }
};

System.out.println(time.with(adjuster)); // 12:30:59Z

OffsetTime with (TemporalField field, long newValue)

Returns a copy of this time with the specified field set to a new value.

final var time = OffsetTime.of(12, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 12:30:15.000000123Z

final var ret1 = time.with(ChronoField.HOUR_OF_DAY, 23);
System.out.println(ret1); // 23:30:15.000000123Z

final var ret2 = time.with(ChronoField.MINUTE_OF_HOUR, 59);
System.out.println(ret2); // 12:59:15.000000123Z

final var ret3 = time.with(ChronoField.SECOND_OF_MINUTE, 30);
System.out.println(ret3); // 12:30:30.000000123Z

final var ret4 = time.with(ChronoField.NANO_OF_SECOND, 456);
System.out.println(ret4); // 12:30:15.000000456Z

final var ret5 = time.with(ChronoField.OFFSET_SECONDS, 3600 * 8);
System.out.println(ret5); // 12:30:15.000000123+08:00

OffsetTime withHour (int hour)

Returns a copy of this OffsetTime with the hour-of-day altered.

final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
System.out.println(time.withHour(23)); // 23:30:15Z

OffsetTime withMinute (int minute)

Returns a copy of this OffsetTime with the minute-of-hour altered.

final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
System.out.println(time.withMinute(59)); // 12:59:15Z

OffsetTime withNano (int nanoOfSecond)

Returns a copy of this OffsetTime with the nano-of-second altered.

final var time = OffsetTime.of(12, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 12:30:15.000000123Z
System.out.println(time.withNano(456789)); // 12:30:15.000456789Z

OffsetTime withOffsetSameInstant (ZoneOffset offset)

Returns a copy of this OffsetTime with the specified offset ensuring that the result is at the same instant on an implied day.

final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z

final var ret = time.withOffsetSameInstant(ZoneOffset.ofHours(4));
System.out.println(ret); // 16:30+04:00
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:30+04:00

final var ret = time.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 08:30Z

OffsetTime withOffsetSameLocal (ZoneOffset offset)

Returns a copy of this OffsetTime with the specified offset ensuring that the result has the same local time.

final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z

final var ret = time.withOffsetSameLocal(ZoneOffset.ofHours(4));
System.out.println(ret); // 12:30+04:00
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:30+04:00

final var ret = time.withOffsetSameLocal(ZoneOffset.UTC);
System.out.println(ret); // 12:30Z

OffsetTime withSecond (int second)

Returns a copy of this OffsetTime with the second-of-minute altered.

final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
System.out.println(time.withSecond(59)); // 12:30:59Z

Related posts

To top of page