Java Instant.atOffset() Method with Examples

Tags: Java Instant Java 8

In this Java core tutorial we learn how to use the java.time.Instant.atOffset() method to convert an Instant object into an OffsetDateTime object in Java programming language.

How to use Instant.atOffset() method

In Java program, with a given Instant object we can use Instant.atOffset(ZoneOffset offset) method to convert it to an OffsetDateTime object in specified time zone as the Java code below.

InstantAtOffsetExample1.java

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class InstantAtOffsetExample1 {
    public static void main(String... args) {
        Instant instant = Instant.parse("2022-07-11T08:00:30Z");

        ZoneOffset utcZoneOffset = ZoneOffset.UTC;
        ZoneOffset sydneyZoneOffset = ZoneId.of("Australia/Sydney").getRules().getOffset(Instant.now());
        ZoneOffset systemZoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());

        OffsetDateTime utcOffsetDateTime = instant.atOffset(utcZoneOffset);
        OffsetDateTime sydneyOffsetDateTime = instant.atOffset(sydneyZoneOffset);
        OffsetDateTime systemOffsetDateTime = instant.atOffset(systemZoneOffset);

        System.out.println("UTC OffsetDateTime: " + utcOffsetDateTime);
        System.out.println("Sydney OffsetDateTime: " + sydneyOffsetDateTime);
        System.out.println("System OffsetDateTime: " + systemOffsetDateTime);
    }
}
The output as below.
UTC OffsetDateTime: 2022-07-11T08:00:30Z
Sydney OffsetDateTime: 2022-07-11T18:00:30+10:00
System OffsetDateTime: 2022-07-11T15:00:30+07:00

Happy Coding 😊

Java Instant.now() Method with Examples

Java Instant.ofEpochSecond() Method with Examples

Java Instant.ofEpochMilli() Method with Examples

Java Instant.parse() Method with Examples

Java Instant.getEpochSecond() Method with Examples

Java Instant.getNano() Method with Examples

Java Instant.plusSeconds() Method with Examples

Java Instant.plusMillis() Method with Examples

Java Instant.plusNanos() Method with Examples

Java Instant.minusSeconds() Method with Examples

Java Instant.minusMillis() Method with Examples

Java Instant.minusNanos() Method with Examples

Java Instant.atZone() Method with Examples

Java Instant.toEpochMilli() Method with Examples

Java Instant.compareTo() Method with Examples

Java Instant.isAfter() Method with Examples

Java Instant.isBefore() Method with Examples

Java Instant.equals() Method with Examples

Java Instant.toString() Method with Examples

Java Compare two Instant Values