Java : OffsetDateTime con ejemplos

OffsetDateTime (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de OffsetDateTime.

Nota :


Summary

Una fecha y hora con una diferencia con respecto a UTC/Greenwich en el sistema de calendario ISO-8601, como 2007-12-03T10:15:30+01:00. (Traducción automática)

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

El OffsetDateTime máximo admitido es '+999999999-12-31T23:59:59.999999999-18:00'. (Traducción automática)

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

static final OffsetDateTime MIN

El OffsetDateTime mínimo admitido es '-999999999-01-01T00:00:00+18:00'. (Traducción automática)

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

Methods

Temporal adjustInto (Temporal temporal)

Ajusta el objeto temporal especificado para que tenga el mismo desplazamiento, fecha y hora que este objeto. (Traducción automática)

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)

Combina esta fecha y hora con una zona horaria para crear un ZonedDateTime garantizando que el resultado tenga el mismo instante. (Traducción automática)

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)

Combina esta fecha y hora con una zona horaria para crear un ZonedDateTime intentando mantener la misma fecha y hora locales. (Traducción automática)

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)

Compara esta fecha y hora con otra fecha y hora. (Traducción automática)

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)

Comprueba si esta fecha y hora es igual a otra fecha y hora. (Traducción automática)

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)

Formatea esta fecha y hora utilizando el formateador especificado. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime de un objeto temporal. (Traducción automática)

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)

Obtiene el valor del campo especificado a partir de esta fecha y hora como un int. (Traducción automática)

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 ()

Obtiene el campo de día del mes. (Traducción automática)

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 ()

Obtiene el campo del día de la semana, que es una enumeración DayOfWeek. (Traducción automática)

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 ()

Obtiene el campo de día del año. (Traducción automática)

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 ()

Obtiene el campo de hora del día. (Traducción automática)

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)

Obtiene el valor del campo especificado a partir de esta fecha y hora como un valor largo. (Traducción automática)

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 ()

Obtiene el campo de minutos de la hora. (Traducción automática)

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 ()

Obtiene el campo de mes del año utilizando la enumeración Mes. (Traducción automática)

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 ()

Obtiene el campo de mes del año del 1 al 12. (Traducción automática)

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 ()

Obtiene el campo de nanosegundos. (Traducción automática)

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 ()

Obtiene el desplazamiento de zona, como '+01:00'. (Traducción automática)

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 ()

Obtiene el campo del segundo minuto. (Traducción automática)

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 ()

Obtiene el campo de año. (Traducción automática)

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 ()

Un código hash para esta fecha y hora. (Traducción automática)

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)

Comprueba si el instante de esta fecha y hora es posterior al de la fecha y hora especificada. (Traducción automática)

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)

Comprueba si el instante de esta fecha y hora es anterior al de la fecha y hora especificada. (Traducción automática)

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)

Comprueba si el instante de esta fecha y hora es igual al de la fecha y hora especificada. (Traducción automática)

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)

Comprueba si el campo especificado es compatible. (Traducción automática)

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)

Comprueba si la unidad especificada es compatible. (Traducción automática)

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)

Devuelve una copia de esta fecha y hora con la cantidad especificada restada. (Traducción automática)

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)

Devuelve una copia de esta fecha y hora con la cantidad especificada restada. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el número especificado de días restados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de horas restadas. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de minutos restados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el número especificado de meses restados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de nanosegundos restados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de segundos restados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el número especificado de semanas restadas. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el número especificado de años restados. (Traducción automática)

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 ()

Obtiene la fecha y hora actual del reloj del sistema en la zona horaria predeterminada. (Traducción automática)

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)

Obtiene la fecha y hora actual del reloj especificado. (Traducción automática)

// 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)

Obtiene la fecha y hora actual del reloj del sistema en la zona horaria especificada. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime de un año, mes, día, hora, minuto, segundo, nanosegundo y desplazamiento. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime a partir de una fecha, hora y desplazamiento. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime a partir de una fecha y hora y un desplazamiento. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime a partir de un ID de zona e instantáneo. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime de una cadena de texto como 2007-12-03T10:15:30+01:00. (Traducción automática)

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)

Obtiene una instancia de OffsetDateTime de una cadena de texto utilizando un formateador específico. (Traducción automática)

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)

Devuelve una copia de esta fecha y hora con la cantidad especificada agregada. (Traducción automática)

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)

Devuelve una copia de esta fecha y hora con la cantidad especificada agregada. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de días agregados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de horas agregadas. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de minutos agregados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de meses agregados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de nanosegundos agregados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de segundos agregados. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de semanas agregadas. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la cantidad especificada de años agregados. (Traducción automática)

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)

Consulta esta fecha y hora utilizando la consulta especificada. (Traducción automática)

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)

Obtiene el rango de valores válidos para el campo especificado. (Traducción automática)

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 ()

Obtiene un comparador que compara dos instancias de OffsetDateTime basándose únicamente en el instante. (Traducción automática)

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 ()

Convierte esta fecha y hora al número de segundos de la época de 1970-01-01T00:00:00Z. (Traducción automática)

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 ()

Convierte esta fecha y hora en un instante. (Traducción automática)

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 ()

Obtiene la parte LocalDate de esta fecha y hora. (Traducción automática)

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 ()

Obtiene la parte LocalDateTime de esta fecha y hora. (Traducción automática)

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 ()

Obtiene la parte LocalTime de esta fecha y hora. (Traducción automática)

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 ()

Convierte esta fecha y hora en un OffsetTime. (Traducción automática)

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 ()

Genera esta fecha y hora como una cadena, como 2007-12-03T10:15:30+01:00. (Traducción automática)

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 ()

Convierte esta fecha y hora en un ZonedDateTime utilizando el desplazamiento como ID de zona. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la hora truncada. (Traducción automática)

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)

Calcula la cantidad de tiempo hasta otra fecha y hora en términos de la unidad especificada. (Traducción automática)

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)

Devuelve una copia ajustada de esta fecha y hora. (Traducción automática)

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)

Devuelve una copia de esta fecha y hora con el campo especificado establecido en un nuevo valor. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el día del mes modificado. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el día del año modificado. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con la hora del día modificada. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el minuto de la hora modificado. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el mes del año modificado. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el nano de segundo alterado. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el desplazamiento especificado, garantizando que el resultado sea en el mismo instante. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el desplazamiento especificado, garantizando que el resultado tenga la misma fecha y hora local. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el segundo de minuto alterado. (Traducción automática)

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)

Devuelve una copia de este OffsetDateTime con el año modificado. (Traducción automática)

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