Java : OffsetDateTime with Examples

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


Summary

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

Class diagram

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

final var dateTime = OffsetDateTime.of(2100, 4, 14, 13, 30, 15, 123000000, offset);
System.out.println(dateTime); // 2100-04-14T13:30:15.123+04:00

System.out.println(dateTime.getYear()); // 2100
System.out.println(dateTime.getMonth()); // APRIL
System.out.println(dateTime.getDayOfMonth()); // 14

System.out.println(dateTime.getHour()); // 13
System.out.println(dateTime.getMinute()); // 30
System.out.println(dateTime.getSecond()); // 15
System.out.println(dateTime.getNano()); // 123000000

System.out.println(dateTime.getOffset()); // +04:00

Fields

static final OffsetDateTime MAX

The maximum supported OffsetDateTime, '+999999999-12-31T23:59:59.999999999-18:00'.

System.out.println(OffsetDateTime.MAX); // +999999999-12-31T23:59:59.999999999-18:00

static final OffsetDateTime MIN

The minimum supported OffsetDateTime, '-999999999-01-01T00:00:00+18:00'.

System.out.println(OffsetDateTime.MIN); // -999999999-01-01T00:00+18:00

Methods

Temporal adjustInto (Temporal temporal)

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

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

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

final var dateTime = OffsetDateTime.of(1800, 10, 20, 1, 2, 3, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1800-10-20T01:02:03Z

final var ret = dateTime.adjustInto(temporal);
System.out.println(ret); // 1800-10-20T01:02:03-01:00[Etc/GMT+1]

ZonedDateTime atZoneSameInstant (ZoneId zone)

Combines this date-time with a time-zone to create a ZonedDateTime ensuring that the result has the same instant.

final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, ZoneOffset.UTC);
System.out.println(datTime); // 2100-01-02T12:00Z

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

final var ret = datTime.atZoneSameInstant(zone);
System.out.println(ret); // 2100-01-02T11:00-01:00[Etc/GMT+1]
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00

final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, offset);
System.out.println(datTime); // 2100-01-02T12:00+09:00

final var ret = datTime.atZoneSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T03:00Z

ZonedDateTime atZoneSimilarLocal (ZoneId zone)

Combines this date-time with a time-zone to create a ZonedDateTime trying to keep the same local date and time.

final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, ZoneOffset.UTC);
System.out.println(datTime); // 2100-01-02T12:00Z

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

final var ret = datTime.atZoneSimilarLocal(zone);
System.out.println(ret); // 2100-01-02T12:00-01:00[Etc/GMT+1]
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00

final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, offset);
System.out.println(datTime); // 2100-01-02T12:00+09:00

final var ret = datTime.atZoneSimilarLocal(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T12:00Z

int compareTo (OffsetDateTime other)

Compares this date-time to another date-time.

final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2100-01-01T00:00Z
System.out.println(dateTime1.compareTo(dateTime2)); // 0
final var offset1 = ZoneOffset.ofHours(2);
final var offset2 = ZoneOffset.ofHours(-4);

final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset1);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset2);

System.out.println(dateTime1); // 2100-01-01T00:00+02:00
System.out.println(dateTime2); // 2100-01-01T00:00-04:00
System.out.println(dateTime1.compareTo(dateTime2)); // -1
final var dateTime1 = OffsetDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 15, 21, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-04-14T15:20Z
System.out.println(dateTime2); // 2100-04-14T15:21Z
System.out.println(dateTime1.compareTo(dateTime2)); // -1
final var dateTime1 = OffsetDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-04-15T00:00Z
System.out.println(dateTime2); // 2100-04-14T00:00Z
System.out.println(dateTime1.compareTo(dateTime2)); // 1
final var dateTime1 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 123, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 456, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-04-13T00:00:00.000000123Z
System.out.println(dateTime2); // 2100-04-13T00:00:00.000000456Z
System.out.println(dateTime1.compareTo(dateTime2)); // -1

boolean equals (Object obj)

Checks if this date-time is equal to another date-time.

final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2100-01-01T00:00Z
System.out.println(dateTime1.equals(dateTime2)); // true
final var offset1 = ZoneOffset.ofHours(2);
final var offset2 = ZoneOffset.ofHours(-4);

final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset1);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset2);

System.out.println(dateTime1); // 2100-01-01T00:00+02:00
System.out.println(dateTime2); // 2100-01-01T00:00-04:00
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = OffsetDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 15, 21, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-04-14T15:20Z
System.out.println(dateTime2); // 2100-04-14T15:21Z
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = OffsetDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-04-15T00:00Z
System.out.println(dateTime2); // 2100-04-14T00:00Z
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 123, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 456, ZoneOffset.UTC);

System.out.println(dateTime1); // 2100-04-13T00:00:00.000000123Z
System.out.println(dateTime2); // 2100-04-13T00:00:00.000000456Z
System.out.println(dateTime1.equals(dateTime2)); // false

String format (DateTimeFormatter formatter)

Formats this date-time using the specified formatter.

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

final var dateTime = OffsetDateTime.of(2099, 4, 7, 13, 30, 45, 0, offset);
System.out.println(dateTime); // 2099-04-07T13:30:45+04:00

{
    final var ret1 = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(ret1); // 2099-04-07T13:30:45+04:00

    final var ret2 = dateTime.format(
            DateTimeFormatter.ofPattern("MM/dd/yyyy, HH:mm:ss O"));
    System.out.println(ret2); // 04/07/2099, 13:30:45 GMT+4
}
{
    System.out.println(Locale.getDefault().toLanguageTag()); // en-US

    final var ret1 = dateTime.format(
            DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
    System.out.println(ret1); // Apr 7, 2099, 1:30:45 PM

    final var ret2 = dateTime.format(
            DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
    System.out.println(ret2); // 4/7/99, 1:30 PM

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

    // Result
    // ↓
    //DateTimeException! : Unable to extract ZoneId from temporal 2099-04-07T13:30:45+04:00
}

static OffsetDateTime from (TemporalAccessor temporal)

Obtains an instance of OffsetDateTime from a temporal object.

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

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

final var ret = OffsetDateTime.from(temporal);
System.out.println(ret); // 2100-01-02T12:30-01:00

int get (TemporalField field)

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

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

final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 45, 123, offset);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123+01:00

System.out.println(dateTime.get(ChronoField.YEAR)); // 2100
System.out.println(dateTime.get(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(dateTime.get(ChronoField.DAY_OF_MONTH)); // 13
System.out.println(dateTime.get(ChronoField.HOUR_OF_DAY)); // 12
System.out.println(dateTime.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(dateTime.get(ChronoField.SECOND_OF_MINUTE)); // 45
System.out.println(dateTime.get(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(dateTime.get(ChronoField.OFFSET_SECONDS)); // 3600

int getDayOfMonth ()

Gets the day-of-month field.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 1
final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 2
final var dateTime = OffsetDateTime.of(2100, 1, 3, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-03T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 3
final var dateTime = OffsetDateTime.of(2100, 1, 30, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-30T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 30
final var dateTime = OffsetDateTime.of(2100, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-31T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 31
final var dateTime = OffsetDateTime.of(2100, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-01T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 1

DayOfWeek getDayOfWeek ()

Gets the day-of-week field, which is an enum DayOfWeek.

final var dateTime = OffsetDateTime.of(2100, 1, 3, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-03T00:00Z
System.out.println(dateTime.getDayOfWeek()); // SUNDAY
final var dateTime = OffsetDateTime.of(2100, 1, 4, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-04T00:00Z
System.out.println(dateTime.getDayOfWeek()); // MONDAY
final var dateTime = OffsetDateTime.of(2100, 1, 5, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-05T00:00Z
System.out.println(dateTime.getDayOfWeek()); // TUESDAY
final var dateTime = OffsetDateTime.of(2100, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-06T00:00Z
System.out.println(dateTime.getDayOfWeek()); // WEDNESDAY
final var dateTime = OffsetDateTime.of(2100, 1, 7, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-07T00:00Z
System.out.println(dateTime.getDayOfWeek()); // THURSDAY
final var dateTime = OffsetDateTime.of(2100, 1, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-08T00:00Z
System.out.println(dateTime.getDayOfWeek()); // FRIDAY
final var dateTime = OffsetDateTime.of(2100, 1, 9, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-09T00:00Z
System.out.println(dateTime.getDayOfWeek()); // SATURDAY
final var dateTime = OffsetDateTime.of(2100, 1, 10, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-10T00:00Z
System.out.println(dateTime.getDayOfWeek()); // SUNDAY

int getDayOfYear ()

Gets the day-of-year field.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getDayOfYear()); // 1
final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z
System.out.println(dateTime.getDayOfYear()); // 2
final var dateTime = OffsetDateTime.of(2100, 1, 30, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-30T00:00Z
System.out.println(dateTime.getDayOfYear()); // 30
final var dateTime = OffsetDateTime.of(2100, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-31T00:00Z
System.out.println(dateTime.getDayOfYear()); // 31
final var dateTime = OffsetDateTime.of(2100, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-01T00:00Z
System.out.println(dateTime.getDayOfYear()); // 32
final var dateTime = OffsetDateTime.of(2100, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-02T00:00Z
System.out.println(dateTime.getDayOfYear()); // 33
final var dateTime = OffsetDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-31T00:00Z
System.out.println(dateTime.getDayOfYear()); // 365
final var dateTime = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-01T00:00Z
System.out.println(dateTime.getDayOfYear()); // 1

int getHour ()

Gets the hour-of-day field.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T14:30:15Z
System.out.println(dateTime.getHour()); // 14

long getLong (TemporalField field)

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

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

final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 45, 123, offset);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123+01:00

System.out.println(dateTime.getLong(ChronoField.YEAR)); // 2100
System.out.println(dateTime.getLong(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(dateTime.getLong(ChronoField.DAY_OF_MONTH)); // 13
System.out.println(dateTime.getLong(ChronoField.HOUR_OF_DAY)); // 12
System.out.println(dateTime.getLong(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(dateTime.getLong(ChronoField.SECOND_OF_MINUTE)); // 45
System.out.println(dateTime.getLong(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(dateTime.getLong(ChronoField.OFFSET_SECONDS)); // 3600

int getMinute ()

Gets the minute-of-hour field.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getMinute()); // 30

Month getMonth ()

Gets the month-of-year field using the Month enum.

final var dateTime = OffsetDateTime.of(2100, 1, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-15T00:00Z
System.out.println(dateTime.getMonth()); // JANUARY
final var dateTime = OffsetDateTime.of(2100, 2, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-15T00:00Z
System.out.println(dateTime.getMonth()); // FEBRUARY
final var dateTime = OffsetDateTime.of(2100, 3, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-03-15T00:00Z
System.out.println(dateTime.getMonth()); // MARCH
final var dateTime = OffsetDateTime.of(2100, 12, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-15T00:00Z
System.out.println(dateTime.getMonth()); // DECEMBER
final var dateTime = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-01T00:00Z
System.out.println(dateTime.getMonth()); // JANUARY

int getMonthValue ()

Gets the month-of-year field from 1 to 12.

final var dateTime = OffsetDateTime.of(2100, 1, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 1
final var dateTime = OffsetDateTime.of(2100, 2, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 2
final var dateTime = OffsetDateTime.of(2100, 3, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-03-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 3
final var dateTime = OffsetDateTime.of(2100, 12, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 12
final var dateTime = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-01T00:00Z
System.out.println(dateTime.getMonthValue()); // 1

int getNano ()

Gets the nano-of-second field.

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, offset);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getNano()); // 0
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 123456789, offset);
System.out.println(dateTime); // 2100-01-01T13:30:15.123456789Z
System.out.println(dateTime.getNano()); // 123456789

ZoneOffset getOffset ()

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

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, offset);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getOffset()); // Z
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(1800, 12, 31, 1, 2, 3, 0, offset);
System.out.println(dateTime); // 1800-12-31T01:02:03+04:00
System.out.println(dateTime.getOffset()); // +04:00

int getSecond ()

Gets the second-of-minute field.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getSecond()); // 15

int getYear ()

Gets the year field.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getYear()); // 2100
final var dateTime = OffsetDateTime.of(-3000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // -3000-01-01T00:00Z
System.out.println(dateTime.getYear()); // -3000
final var dateTime = OffsetDateTime.of(9999, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 9999-01-01T00:00Z
System.out.println(dateTime.getYear()); // 9999

int hashCode ()

A hash code for this date-time.

final var offset1 = ZoneOffset.ofHours(4);
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 1, 0, 0, 0, offset1);
System.out.println(dateTime1.hashCode()); // 821637959

final var offset2 = ZoneOffset.UTC;
final var dateTime2 = OffsetDateTime.of(1800, 12, 31, 23, 59, 59, 123, offset2);
System.out.println(dateTime2.hashCode()); // 1435253744

boolean isAfter (OffsetDateTime other)

Checks if the instant of this date-time is after that of the specified date-time.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var dateTime2 = OffsetDateTime.of(2099, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime3 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime2); // 2099-01-01T00:00Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2101-01-01T00:00Z

System.out.println(dateTime.isAfter(dateTime2)); // true
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // false
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(dateTime); // 1800-01-01T00:00+04:00

final var dateTime2 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
final var dateTime3 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
final var dateTime4 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(5));

System.out.println(dateTime2); // 1800-01-01T00:00+03:00
System.out.println(dateTime3); // 1800-01-01T00:00+04:00
System.out.println(dateTime4); // 1800-01-01T00:00+05:00

System.out.println(dateTime.isAfter(dateTime2)); // false
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // true

boolean isBefore (OffsetDateTime other)

Checks if the instant of this date-time is before that of the specified date-time.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var dateTime2 = OffsetDateTime.of(2099, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime3 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime2); // 2099-01-01T00:00Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2101-01-01T00:00Z

System.out.println(dateTime.isBefore(dateTime2)); // false
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // true
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(dateTime); // 1800-01-01T00:00+04:00

final var dateTime2 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
final var dateTime3 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
final var dateTime4 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(5));

System.out.println(dateTime2); // 1800-01-01T00:00+03:00
System.out.println(dateTime3); // 1800-01-01T00:00+04:00
System.out.println(dateTime4); // 1800-01-01T00:00+05:00

System.out.println(dateTime.isBefore(dateTime2)); // true
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // false

boolean isEqual (OffsetDateTime other)

Checks if the instant of this date-time is equal to that of the specified date-time.

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var dateTime2 = OffsetDateTime.of(2099, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime3 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

System.out.println(dateTime2); // 2099-01-01T00:00Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2101-01-01T00:00Z

System.out.println(dateTime.isEqual(dateTime2)); // false
System.out.println(dateTime.isEqual(dateTime3)); // true
System.out.println(dateTime.isEqual(dateTime4)); // false
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(dateTime); // 1800-01-01T00:00+04:00

final var dateTime2 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
final var dateTime3 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
final var dateTime4 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(5));

System.out.println(dateTime2); // 1800-01-01T00:00+03:00
System.out.println(dateTime3); // 1800-01-01T00:00+04:00
System.out.println(dateTime4); // 1800-01-01T00:00+05:00

System.out.println(dateTime.isEqual(dateTime2)); // false
System.out.println(dateTime.isEqual(dateTime3)); // true
System.out.println(dateTime.isEqual(dateTime4)); // false

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T12:30Z

System.out.println(dateTime.isSupported(ChronoField.YEAR)); // true
System.out.println(dateTime.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(dateTime.isSupported(ChronoField.DAY_OF_MONTH)); // true

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

System.out.println(dateTime.isSupported(ChronoField.OFFSET_SECONDS)); // true

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T12:30Z

System.out.println(dateTime.isSupported(ChronoUnit.YEARS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.MONTHS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.WEEKS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.DAYS)); // true

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

System.out.println(dateTime.isSupported(ChronoUnit.FOREVER)); // false

OffsetDateTime minus (long amountToSubtract, TemporalUnit unit)

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

final var dateTime = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T00:00Z

final var ret1 = dateTime.minus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2099-04-13T00:00Z

final var ret2 = dateTime.minus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-02-13T00:00Z

final var ret3 = dateTime.minus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-04-10T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z

final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T09:20:30.000000123Z

final var ret2 = dateTime.minus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:18:30.000000123Z

final var ret3 = dateTime.minus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:27.000000123Z

final var ret4 = dateTime.minus(4, ChronoUnit.NANOS);
System.out.println(ret4); // 2100-01-01T10:20:30.000000119Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2099-12-31T23:00Z

final var ret2 = dateTime.minus(365, ChronoUnit.DAYS);
System.out.println(ret2); // 2099-01-01T00:00Z

final var ret3 = dateTime.minus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // -0900-01-01T00:00Z

OffsetDateTime minus (TemporalAmount amountToSubtract)

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

final var dateTime = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T00:00Z

final var ret1 = dateTime.minus(Period.ofYears(1));
System.out.println(ret1); // 2099-04-13T00:00Z

final var ret2 = dateTime.minus(Period.ofMonths(2));
System.out.println(ret2); // 2100-02-13T00:00Z

final var ret3 = dateTime.minus(Period.ofDays(3));
System.out.println(ret3); // 2100-04-10T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z

final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T09:20:30.000000123Z

final var ret2 = dateTime.minus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:18:30.000000123Z

final var ret3 = dateTime.minus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:27.000000123Z

final var ret4 = dateTime.minus(Duration.ofNanos(4));
System.out.println(ret4); // 2100-01-01T10:20:30.000000119Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2099-12-31T23:00Z

final var ret2 = dateTime.minus(Period.ofDays(365));
System.out.println(ret2); // 2099-01-01T00:00Z

final var ret3 = dateTime.minus(Period.ofYears(3000));
System.out.println(ret3); // -0900-01-01T00:00Z

OffsetDateTime minusDays (long days)

Returns a copy of this OffsetDateTime with the specified number of days subtracted.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.minusDays(1)); // 2100-05-19T00:00Z
System.out.println(dateTime.minusDays(2)); // 2100-05-18T00:00Z
System.out.println(dateTime.minusDays(20)); // 2100-04-30T00:00Z
System.out.println(dateTime.minusDays(365)); // 2099-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

System.out.println(dateTime.minusDays(1)); // 2099-12-31T00:00Z

OffsetDateTime minusHours (long hours)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z

System.out.println(dateTime.minusHours(1)); // 2100-01-01T12:30:15Z
System.out.println(dateTime.minusHours(2)); // 2100-01-01T11:30:15Z
System.out.println(dateTime.minusHours(13)); // 2100-01-01T00:30:15Z
System.out.println(dateTime.minusHours(14)); // 2099-12-31T23:30:15Z

OffsetDateTime minusMinutes (long minutes)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30Z

System.out.println(dateTime.minusMinutes(1)); // 2100-01-01T13:29Z
System.out.println(dateTime.minusMinutes(2)); // 2100-01-01T13:28Z
System.out.println(dateTime.minusMinutes(30)); // 2100-01-01T13:00Z
System.out.println(dateTime.minusMinutes(31)); // 2100-01-01T12:59Z
System.out.println(dateTime.minusMinutes(60 * 14)); // 2099-12-31T23:30Z

OffsetDateTime minusMonths (long months)

Returns a copy of this OffsetDateTime with the specified number of months subtracted.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.minusMonths(1)); // 2100-04-20T00:00Z
System.out.println(dateTime.minusMonths(2)); // 2100-03-20T00:00Z
System.out.println(dateTime.minusMonths(12)); // 2099-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2101, 3, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-03-31T00:00Z

System.out.println(dateTime.minusMonths(1)); // 2101-02-28T00:00Z

OffsetDateTime minusNanos (long nanos)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123Z

System.out.println(dateTime.minusNanos(1)); // 2100-01-01T13:30:15.000000122Z
System.out.println(dateTime.minusNanos(2)); // 2100-01-01T13:30:15.000000121Z
System.out.println(dateTime.minusNanos(123)); // 2100-01-01T13:30:15Z
System.out.println(dateTime.minusNanos(124)); // 2100-01-01T13:30:14.999999999Z

OffsetDateTime minusSeconds (long seconds)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z

System.out.println(dateTime.minusSeconds(1)); // 2100-01-01T13:30:14Z
System.out.println(dateTime.minusSeconds(2)); // 2100-01-01T13:30:13Z
System.out.println(dateTime.minusSeconds(15)); // 2100-01-01T13:30Z
System.out.println(dateTime.minusSeconds(16)); // 2100-01-01T13:29:59Z
System.out.println(dateTime.minusSeconds(60 * 60)); // 2100-01-01T12:30:15Z
System.out.println(dateTime.minusSeconds(60 * 60 * 24)); // 2099-12-31T13:30:15Z

OffsetDateTime minusWeeks (long weeks)

Returns a copy of this OffsetDateTime with the specified number of weeks subtracted.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.minusWeeks(1)); //2100-05-13T00:00Z
System.out.println(dateTime.minusWeeks(2)); // 2100-05-06T00:00Z
System.out.println(dateTime.minusWeeks(20)); // 2099-12-31T00:00Z
System.out.println(dateTime.minusWeeks(100)); // 2098-06-19T00:00Z

OffsetDateTime minusYears (long years)

Returns a copy of this OffsetDateTime with the specified number of years subtracted.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.minusYears(1)); // 2099-05-20T00:00Z
System.out.println(dateTime.minusYears(2)); // 2098-05-20T00:00Z
System.out.println(dateTime.minusYears(10)); // 2090-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2104, 2, 29, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2104-02-29T00:00Z
System.out.println(dateTime.minusYears(1)); // 2103-02-28T00:00Z

static OffsetDateTime now ()

Obtains the current date-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(OffsetDateTime.now()); // 2024-07-16T00:07:24.395528100-07:00

static OffsetDateTime now (Clock clock)

Obtains the current date-time from the specified clock.

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

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

final var dateTime1 = OffsetDateTime.now();
System.out.println(dateTime1); // 2024-07-16T00:08:23.778654300-07:00

final var dateTime2 = OffsetDateTime.now(clock);
System.out.println(dateTime2); // 2024-07-21T00:08:23.779653500-07:00

static OffsetDateTime now (ZoneId zone)

Obtains the current date-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 dateTime1 = OffsetDateTime.now(zone1);
System.out.println(dateTime1); // 2024-07-16T07:13:17.397503100Z

final var dateTime2 = OffsetDateTime.now(zone2);
System.out.println(dateTime2); // 2024-07-16T06:13:17.397503100-01:00

static OffsetDateTime of (int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)

Obtains an instance of OffsetDateTime from a year, month, day, hour, minute, second, nanosecond and offset.

final var ret1 = OffsetDateTime.of(2100, 1, 2, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(ret1); // 2100-01-02T12:30Z

final var ret2 = OffsetDateTime.of(1800, 12, 31, 1, 2, 3, 456, ZoneOffset.ofHours(4));
System.out.println(ret2); // 1800-12-31T01:02:03.000000456+04:00

static OffsetDateTime of (LocalDate date, LocalTime time, ZoneOffset offset)

Obtains an instance of OffsetDateTime from a date, time and offset.

final var date = LocalDate.of(2100, 1, 2);
System.out.println(date); // 2100-01-02

final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30

final var ret = OffsetDateTime.of(date, time, ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T12:30Z
final var date = LocalDate.of(1800, 12, 31);
System.out.println(date); // 1800-12-31

final var time = LocalTime.of(1, 2, 3, 456);
System.out.println(time); // 01:02:03.000000456

final var ret = OffsetDateTime.of(date, time, ZoneOffset.ofHours(4));
System.out.println(ret); // 1800-12-31T01:02:03.000000456+04:00

static OffsetDateTime of (LocalDateTime dateTime, ZoneOffset offset)

Obtains an instance of OffsetDateTime from a date-time and offset.

final var dateTime = LocalDateTime.of(2100, 1, 2, 12, 30);
System.out.println(dateTime); // 2100-01-02T12:30

final var ret = OffsetDateTime.of(dateTime, ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T12:30Z
final var dateTime = LocalDateTime.of(1800, 12, 31, 1, 2, 3, 456);
System.out.println(dateTime); // 1800-12-31T01:02:03.000000456

final var ret = OffsetDateTime.of(dateTime, ZoneOffset.ofHours(4));
System.out.println(ret); // 1800-12-31T01:02:03.000000456+04:00

static OffsetDateTime ofInstant (Instant instant, ZoneId zone)

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

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

final var dateTime1 = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z

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

final var dateTime2 = OffsetDateTime.ofInstant(instant, zone);
System.out.println(dateTime2); // 2099-12-31T23:00-01:00

static OffsetDateTime parse (CharSequence text)

Obtains an instance of OffsetDateTime from a text string such as 2007-12-03T10:15:30+01:00.

final var ret1 = OffsetDateTime.parse("2100-04-14T00:00Z");
System.out.println(ret1); // 2100-04-14T00:00Z

final var ret2 = OffsetDateTime.parse("2100-04-14T13:30:45Z");
System.out.println(ret2); // 2100-04-14T13:30:45Z

final var ret3 = OffsetDateTime.parse("2100-04-14T13:30:45.123456789Z");
System.out.println(ret3); // 2100-04-14T13:30:45.123456789Z

final var ret4 = OffsetDateTime.parse("1800-01-01T01:02:03+09:00");
System.out.println(ret4); // 1800-01-01T01:02:03+09:00
try {
    final var ret = OffsetDateTime.parse("2100-04-14T00:00");
} catch (DateTimeParseException e) {
    System.out.println("DateTimeParseException! : " + e.getMessage());
}

// Result
// ↓
//DateTimeParseException! : Text '2100-04-14T00:00' could not be parsed at index 16

static OffsetDateTime parse (CharSequence text, DateTimeFormatter formatter)

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

final var ret1 = OffsetDateTime.parse(
        "2100-04-14T12:30:45Z", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(ret1); // 2100-04-14T12:30:45Z

final var ret2 = OffsetDateTime.parse(
        "04/07/1800, 13:30:45 GMT+4",
        DateTimeFormatter.ofPattern("MM/dd/yyyy, HH:mm:ss O"));
System.out.println(ret2); // 1800-04-07T13:30:45+04:00

OffsetDateTime plus (long amountToAdd, TemporalUnit unit)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var ret1 = dateTime.plus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2101-01-01T00:00Z

final var ret2 = dateTime.plus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-03-01T00:00Z

final var ret3 = dateTime.plus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-01-04T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z

final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T11:20:30.000000123Z

final var ret2 = dateTime.plus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:22:30.000000123Z

final var ret3 = dateTime.plus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:33.000000123Z

final var ret4 = dateTime.plus(4, ChronoUnit.NANOS);
System.out.println(ret4); // 2100-01-01T10:20:30.000000127Z
final var dateTime = OffsetDateTime.of(2099, 12, 31, 23, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-12-31T23:00Z

final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T00:00Z

final var ret2 = dateTime.plus(366, ChronoUnit.DAYS);
System.out.println(ret2); // 2101-01-01T23:00Z

final var ret3 = dateTime.plus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // 5099-12-31T23:00Z

OffsetDateTime plus (TemporalAmount amountToAdd)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z

final var ret1 = dateTime.plus(Period.ofYears(1));
System.out.println(ret1); // 2101-01-01T00:00Z

final var ret2 = dateTime.plus(Period.ofMonths(2));
System.out.println(ret2); // 2100-03-01T00:00Z

final var ret3 = dateTime.plus(Period.ofDays(3));
System.out.println(ret3); // 2100-01-04T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z

final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T11:20:30.000000123Z

final var ret2 = dateTime.plus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:22:30.000000123Z

final var ret3 = dateTime.plus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:33.000000123Z

final var ret4 = dateTime.plus(Duration.ofNanos(4));
System.out.println(ret4); // 2100-01-01T10:20:30.000000127Z
final var dateTime = OffsetDateTime.of(2099, 12, 31, 23, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-12-31T23:00Z

final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T00:00Z

final var ret2 = dateTime.plus(Period.ofDays(366));
System.out.println(ret2); // 2101-01-01T23:00Z

final var ret3 = dateTime.plus(Period.ofYears(3000));
System.out.println(ret3); // 5099-12-31T23:00Z

OffsetDateTime plusDays (long days)

Returns a copy of this OffsetDateTime with the specified number of days added.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.plusDays(1)); // 2100-05-21T00:00Z
System.out.println(dateTime.plusDays(2)); // 2100-05-22T00:00Z
System.out.println(dateTime.plusDays(12)); // 2100-06-01T00:00Z
System.out.println(dateTime.plusDays(365)); // 2101-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-31T00:00Z

System.out.println(dateTime.plusDays(1)); // 2101-01-01T00:00Z

OffsetDateTime plusHours (long hours)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:00Z

System.out.println(dateTime.plusHours(1)); // 2100-01-01T14:00Z
System.out.println(dateTime.plusHours(2)); // 2100-01-01T15:00Z
System.out.println(dateTime.plusHours(11)); // 2100-01-02T00:00Z
System.out.println(dateTime.plusHours(12)); // 2100-01-02T01:00Z

OffsetDateTime plusMinutes (long minutes)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30Z

System.out.println(dateTime.plusMinutes(1)); // 2100-01-01T13:31Z
System.out.println(dateTime.plusMinutes(2)); // 2100-01-01T13:32Z
System.out.println(dateTime.plusMinutes(30)); // 2100-01-01T14:00Z
System.out.println(dateTime.plusMinutes(31)); // 2100-01-01T14:01Z
System.out.println(dateTime.plusMinutes(60 * 24)); // 2100-01-02T13:30Z

OffsetDateTime plusMonths (long months)

Returns a copy of this OffsetDateTime with the specified number of months added.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.plusMonths(1)); // 2100-06-20T00:00Z
System.out.println(dateTime.plusMonths(2)); // 2100-07-20T00:00Z
System.out.println(dateTime.plusMonths(12)); // 2101-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2100, 3, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-03-31T00:00Z

System.out.println(dateTime.plusMonths(1)); // 2100-04-30T00:00Z

OffsetDateTime plusNanos (long nanos)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123Z

final var ret1 = dateTime.plusNanos(1);
System.out.println(ret1); // 2100-01-01T13:30:15.000000124Z

final var ret2 = dateTime.plusNanos(2);
System.out.println(ret2); // 2100-01-01T13:30:15.000000125Z

final var ret3 = dateTime.plusNanos(1000000000L - 124);
System.out.println(ret3); // 2100-01-01T13:30:15.999999999Z

final var ret4 = dateTime.plusNanos(1000000000L - 123);
System.out.println(ret4); // 2100-01-01T13:30:16Z

OffsetDateTime plusSeconds (long seconds)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z

System.out.println(dateTime.plusSeconds(1)); // 2100-01-01T13:30:16Z
System.out.println(dateTime.plusSeconds(2)); // 2100-01-01T13:30:17Z
System.out.println(dateTime.plusSeconds(45)); // 2100-01-01T13:31Z
System.out.println(dateTime.plusSeconds(46)); // 2100-01-01T13:31:01Z
System.out.println(dateTime.plusSeconds(60 + 45)); // 2100-01-01T13:32Z
System.out.println(dateTime.plusSeconds(60 * 60)); // 2100-01-01T14:30:15Z
System.out.println(dateTime.plusSeconds(60 * 60 * 24)); // 2100-01-02T13:30:15Z

OffsetDateTime plusWeeks (long weeks)

Returns a copy of this OffsetDateTime with the specified number of weeks added.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.plusWeeks(1)); // 2100-05-27T00:00Z
System.out.println(dateTime.plusWeeks(2)); // 2100-06-03T00:00Z
System.out.println(dateTime.plusWeeks(20)); // 2100-10-07T00:00Z
System.out.println(dateTime.plusWeeks(100)); // 2102-04-20T00:00Z

OffsetDateTime plusYears (long years)

Returns a copy of this OffsetDateTime with the specified number of years added.

final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z

System.out.println(dateTime.plusYears(1)); // 2101-05-20T00:00Z
System.out.println(dateTime.plusYears(2)); // 2102-05-20T00:00Z
System.out.println(dateTime.plusYears(10)); // 2110-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2104, 2, 29, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2104-02-29T00:00Z
System.out.println(dateTime.plusYears(1)); // 2105-02-28T00:00Z

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

Queries this date-time using the specified query.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30Z

System.out.println(dateTime.query(TemporalQueries.localDate())); // 2100-04-14
System.out.println(dateTime.query(TemporalQueries.localTime())); // 12:30
System.out.println(dateTime.query(TemporalQueries.offset())); // Z
System.out.println(dateTime.query(TemporalQueries.precision())); // Nanos

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T00:00Z
System.out.println(dateTime.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(dateTime.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(dateTime.range(ChronoField.DAY_OF_MONTH)); // 1 - 30

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

System.out.println(dateTime.range(ChronoField.OFFSET_SECONDS)); // -64800 - 64800
final var dateTime1 = OffsetDateTime.of(2103, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2103-02-01T00:00Z

System.out.println(dateTime1.toLocalDate().isLeapYear()); // false
System.out.println(dateTime1.range(ChronoField.DAY_OF_MONTH)); // 1 - 28

final var dateTime2 = OffsetDateTime.of(2104, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2104-02-01T00:00Z

System.out.println(dateTime2.toLocalDate().isLeapYear()); // true
System.out.println(dateTime2.range(ChronoField.DAY_OF_MONTH)); // 1 - 29

static Comparator<OffsetDateTime> timeLineOrder ()

Gets a comparator that compares two OffsetDateTime instances based solely on the instant.

final var dateTime = OffsetDateTime.of(2100, 12, 3, 11, 0, 0, 0, ZoneOffset.ofHours(1));
System.out.println(dateTime); // 2100-12-03T11:00+01:00

final var dateTime1 = OffsetDateTime.of(2100, 12, 3, 10, 30, 0, 0, ZoneOffset.ofHours(1));
final var dateTime2 = OffsetDateTime.of(2100, 12, 3, 12, 0, 0, 0, ZoneOffset.ofHours(2));
final var dateTime3 = OffsetDateTime.of(2100, 12, 3, 11, 30, 0, 0, ZoneOffset.ofHours(1));

System.out.println(dateTime1); // 2100-12-03T10:30+01:00
System.out.println(dateTime2); // 2100-12-03T12:00+02:00
System.out.println(dateTime3); // 2100-12-03T11:30+01:00

final var comparator = OffsetDateTime.timeLineOrder();

System.out.println(comparator.compare(dateTime, dateTime1)); // 1
System.out.println(comparator.compare(dateTime, dateTime2)); // 0
System.out.println(comparator.compare(dateTime, dateTime3)); // -1

System.out.println(dateTime.compareTo(dateTime1)); // 1
System.out.println(dateTime.compareTo(dateTime2)); // -1
System.out.println(dateTime.compareTo(dateTime3)); // -1

long toEpochSecond ()

Converts this date-time to the number of seconds from the epoch of 1970-01-01T00:00:00Z.

final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z

final long ret = dateTime.toEpochSecond();
System.out.println(ret); // 4102531200
System.out.println(Instant.ofEpochSecond(ret)); // 2100-01-02T00:00:00Z
final var offset = ZoneOffset.ofHours(8);
System.out.println(offset); // +08:00

final var dateTime = OffsetDateTime.of(1800, 12, 31, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 1800-12-31T12:30+08:00

final long ret = dateTime.toEpochSecond();
System.out.println(ret); // -5333196600
System.out.println(Instant.ofEpochSecond(ret)); // 1800-12-31T04:30:00Z

Instant toInstant ()

Converts this date-time to an Instant.

final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z

final var ret = dateTime.toInstant();
System.out.println(ret); // 2100-01-02T00:00:00Z
final var offset = ZoneOffset.ofHours(8);
System.out.println(offset); // +08:00

final var dateTime = OffsetDateTime.of(1800, 12, 31, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 1800-12-31T12:30+08:00

final var ret = dateTime.toInstant();
System.out.println(ret); // 1800-12-31T04:30:00Z

LocalDate toLocalDate ()

Gets the LocalDate part of this date-time.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toLocalDate()); // 2100-04-14
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toLocalDate()); // 1800-01-01

LocalDateTime toLocalDateTime ()

Gets the LocalDateTime part of this date-time.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toLocalDateTime()); // 2100-04-14T12:30:45
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toLocalDateTime()); // 1800-01-01T00:00

LocalTime toLocalTime ()

Gets the LocalTime part of this date-time.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toLocalTime()); // 12:30:45
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toLocalTime()); // 00:00

OffsetTime toOffsetTime ()

Converts this date-time to an OffsetTime.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toOffsetTime()); // 12:30:45Z
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toOffsetTime()); // 00:00+04:00

String toString ()

Outputs this date-time as a String, such as 2007-12-03T10:15:30+01:00.

final var offset1 = ZoneOffset.UTC;
final var str1 = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, offset1).toString();
System.out.println(str1); // 2100-01-02T00:00Z

final var offset2 = ZoneOffset.ofHours(4);
final var str2 = OffsetDateTime.of(1800, 12, 31, 1, 2, 3, 456, offset2).toString();
System.out.println(str2); // 1800-12-31T01:02:03.000000456+04:00

ZonedDateTime toZonedDateTime ()

Converts this date-time to a ZonedDateTime using the offset as the zone ID.

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toZonedDateTime()); // 2100-04-14T12:30:45Z
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toZonedDateTime()); // 1800-01-01T00:00+04:00

OffsetDateTime truncatedTo (TemporalUnit unit)

Returns a copy of this OffsetDateTime with the time truncated.

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

final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 15, 123456789, offset);
System.out.println(dateTime); // 2100-04-14T12:30:15.123456789Z

System.out.println(dateTime.truncatedTo(ChronoUnit.DAYS)); // 2100-04-14T00:00Z

System.out.println(dateTime.truncatedTo(ChronoUnit.HOURS)); // 2100-04-14T12:00Z
System.out.println(dateTime.truncatedTo(ChronoUnit.MINUTES)); // 2100-04-14T12:30Z
System.out.println(dateTime.truncatedTo(ChronoUnit.SECONDS)); // 2100-04-14T12:30:15Z
System.out.println(dateTime.truncatedTo(ChronoUnit.NANOS)); // 2100-04-14T12:30:15.123456789Z

long until (Temporal endExclusive, TemporalUnit unit)

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

final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2101, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2101-02-02T00:00Z

System.out.println(dateTime1.until(dateTime2, ChronoUnit.YEARS)); // 1
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MONTHS)); // 13
System.out.println(dateTime1.until(dateTime2, ChronoUnit.DAYS)); // 397
System.out.println(dateTime1.until(dateTime2, ChronoUnit.HOURS)); // 9528
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // 571680
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // 34300800
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // 34300800000000000
final var dateTime1 = OffsetDateTime.of(2100, 8, 14, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 6, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-08-14T00:00Z
System.out.println(dateTime2); // 2100-06-15T00:00Z

System.out.println(dateTime1.until(dateTime2, ChronoUnit.YEARS)); // 0
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MONTHS)); // -1
System.out.println(dateTime1.until(dateTime2, ChronoUnit.DAYS)); // -60
System.out.println(dateTime1.until(dateTime2, ChronoUnit.HOURS)); // -1440
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // -86400
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // -5184000
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // -5184000000000000
final var offset1 = ZoneOffset.ofHours(3);
final var offset2 = ZoneOffset.ofHours(-5);
final var dateTime1 = OffsetDateTime.of(1999, 1, 1, 1, 0, 0, 0, offset1);
final var dateTime2 = OffsetDateTime.of(1999, 1, 1, 1, 0, 0, 0, offset2);
System.out.println(dateTime1); // 1999-01-01T01:01:01+03:00
System.out.println(dateTime2); // 1999-01-01T01:01:01-05:00

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

OffsetDateTime with (TemporalAdjuster adjuster)

Returns an adjusted copy of this date-time.

final var dateTime = OffsetDateTime.of(2100, 1, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-15T00:00Z

final var ret1 = dateTime.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ret1); // 2100-01-01T00:00Z

final var ret2 = dateTime.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(ret2); // 2100-01-31T00:00Z
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30Z

System.out.println(dateTime.with(Year.of(1800))); // 1800-04-14T12:30Z
System.out.println(dateTime.with(Month.JANUARY)); // 2100-01-14T12:30Z
System.out.println(dateTime.with(LocalTime.MAX)); // 2100-04-14T23:59:59.999999999Z
System.out.println(dateTime.with(LocalDate.MIN)); // -999999999-01-01T12:30Z

OffsetDateTime with (TemporalField field, long newValue)

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

final var dateTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T12:00Z

final var ret1 = dateTime.with(ChronoField.YEAR, 1800);
System.out.println(ret1); // 1800-01-02T12:00Z

final var ret2 = dateTime.with(ChronoField.MONTH_OF_YEAR, 12);
System.out.println(ret2); // 2100-12-02T12:00Z

final var ret3 = dateTime.with(ChronoField.DAY_OF_MONTH, 30);
System.out.println(ret3); // 2100-01-30T12:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 45, 123000000, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30:45.123Z

final var ret1 = dateTime.with(ChronoField.HOUR_OF_DAY, 23);
System.out.println(ret1); // 2100-01-01T23:30:45.123Z

final var ret2 = dateTime.with(ChronoField.MINUTE_OF_HOUR, 59);
System.out.println(ret2); // 2100-01-01T12:59:45.123Z

final var ret3 = dateTime.with(ChronoField.SECOND_OF_MINUTE, 0);
System.out.println(ret3); // 2100-01-01T12:30:00.123Z

final var ret4 = dateTime.with(ChronoField.NANO_OF_SECOND, 456);
System.out.println(ret4); // 2100-01-01T12:30:45.000000456Z

OffsetDateTime withDayOfMonth (int dayOfMonth)

Returns a copy of this OffsetDateTime with the day-of-month altered.

final var dateTime = OffsetDateTime.of(2100, 4, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T00:00Z

System.out.println(dateTime.withDayOfMonth(1)); // 2100-04-01T00:00Z
System.out.println(dateTime.withDayOfMonth(2)); // 2100-04-02T00:00Z
System.out.println(dateTime.withDayOfMonth(29)); // 2100-04-29T00:00Z
System.out.println(dateTime.withDayOfMonth(30)); // 2100-04-30T00:00Z

OffsetDateTime withDayOfYear (int dayOfYear)

Returns a copy of this OffsetDateTime with the day-of-year altered.

final var dateTime = OffsetDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z

System.out.println(dateTime.withDayOfYear(1)); // 2100-01-01T12:30Z
System.out.println(dateTime.withDayOfYear(2)); // 2100-01-02T12:30Z
System.out.println(dateTime.withDayOfYear(30)); // 2100-01-30T12:30Z
System.out.println(dateTime.withDayOfYear(31)); // 2100-01-31T12:30Z
System.out.println(dateTime.withDayOfYear(32)); // 2100-02-01T12:30Z
System.out.println(dateTime.withDayOfYear(33)); // 2100-02-02T12:30Z

System.out.println(dateTime.withDayOfYear(365)); // 2100-12-31T12:30Z

OffsetDateTime withHour (int hour)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30Z

System.out.println(dateTime.withHour(0)); // 2100-01-01T00:30Z
System.out.println(dateTime.withHour(23)); // 2100-01-01T23:30Z

OffsetDateTime withMinute (int minute)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30Z

System.out.println(dateTime.withMinute(0)); // 2100-01-01T12:00Z
System.out.println(dateTime.withMinute(59)); // 2100-01-01T12:59Z

OffsetDateTime withMonth (int month)

Returns a copy of this OffsetDateTime with the month-of-year altered.

final var dateTime = OffsetDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z

System.out.println(dateTime.withMonth(1)); // 2100-01-08T12:30Z
System.out.println(dateTime.withMonth(2)); // 2100-02-08T12:30Z
System.out.println(dateTime.withMonth(11)); // 2100-11-08T12:30Z
System.out.println(dateTime.withMonth(12)); // 2100-12-08T12:30Z

OffsetDateTime withNano (int nanoOfSecond)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00:00.000000123Z

System.out.println(dateTime.withNano(0)); // 2100-01-01T00:00Z
System.out.println(dateTime.withNano(999999999)); // 2100-01-01T00:00:00.999999999Z

OffsetDateTime withOffsetSameInstant (ZoneOffset offset)

Returns a copy of this OffsetDateTime with the specified offset ensuring that the result is at the same instant.

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30Z

final var ret = dateTime.withOffsetSameInstant(ZoneOffset.ofHours(4));
System.out.println(ret); // 2100-01-01T16:30+04:00
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30+04:00

final var ret = dateTime.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T08:30Z

OffsetDateTime withOffsetSameLocal (ZoneOffset offset)

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

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30Z

final var ret = dateTime.withOffsetSameLocal(ZoneOffset.ofHours(4));
System.out.println(ret); // 2100-01-01T12:30+04:00
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30+04:00

final var ret = dateTime.withOffsetSameLocal(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T12:30Z

OffsetDateTime withSecond (int second)

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

final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30:45Z

System.out.println(dateTime.withSecond(0)); // 2100-01-01T12:30Z
System.out.println(dateTime.withSecond(1)); // 2100-01-01T12:30:01Z
System.out.println(dateTime.withSecond(59)); // 2100-01-01T12:30:59Z

OffsetDateTime withYear (int year)

Returns a copy of this OffsetDateTime with the year altered.

final var dateTime = OffsetDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z

System.out.println(dateTime.withYear(1800)); // 1800-04-08T12:30Z
System.out.println(dateTime.withYear(-999)); // -0999-04-08T12:30Z

Related posts

To top of page