Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

time.OffsetDateTime: practical reference

time.OffsetDateTime: practical reference: OffsetDateTime combines local date-time with a fixed UTC offset, identifying an instant without regional zone rules.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.time.OffsetDateTime

OffsetDateTime combines local date-time with a fixed UTC offset, identifying an instant without regional zone rules.

Use OffsetDateTime when a database or wire value must preserve both local date-time fields and the fixed UTC offset that identifies its instant.

An offset is not a region time zone and carries no daylight-saving history. equals includes the local fields and offset, whereas isEqual compares the represented instant.

withOffsetSameInstant changes local fields to preserve the instant; withOffsetSameLocal replaces only the offset and therefore changes the instant. Offsets are limited to plus or minus 18 hours.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var value = java.time.OffsetDateTime.parse("2024-01-01T12:00:00+02:00");
        System.out.println(value.toInstant());
    }
}

Result

2024-01-01T10:00:00Z

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources