Java Convert LocalDateTime to OffsetDateTime

Tags: Java LocalDateTime Java OffsetDateTime Java 8

In this Java core tutorial we learn how to convert a java.time.LocalDateTime object to a java.time.OffsetDateTime object in Java programming language.

Table of contents

  1. Convert LocalDateTime to OffsetDateTime with System Default Time Zone
  2. Convert LocalDateTime to OffsetDateTime with Specified Time Zone

Convert LocalDateTime to OffsetDateTime with System Default Time Zone

With a given LocalDateTime object in Java we can use the atOffset() method and provide ZoneOffset object to convert it to an OffsetDateTime object.

In the following Java program we show how to convert a LocalDateTime object to OffsetDateTime object with ZoneOffset based on system default time zone.

ConvertLocalDateTimeToOffsetDateTimeExample1.java

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

public class ConvertLocalDateTimeToOffsetDateTimeExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
        OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffset);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("OffsetDateTime: " + offsetDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-24T11:20:56.554407100
OffsetDateTime: 2022-04-24T11:20:56.554407100+07:00

Convert LocalDateTime to OffsetDateTime with Specified Time Zone

In the following Java program we show how to convert a LocalDateTime object to OffsetDateTime object with ZoneOffset value from a specified time zone Australia/Sydney.

ConvertLocalDateTimeToOffsetDateTimeExample2.java

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

public class ConvertLocalDateTimeToOffsetDateTimeExample2 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset zoneOffset = ZoneId.of("Australia/Sydney").getRules().getOffset(Instant.now());
        OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffset);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("OffsetDateTime: " + offsetDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-24T11:22:13.232470600
OffsetDateTime: 2022-04-24T11:22:13.232470600+10:00

Happy Coding 😊

Java Convert LocalDateTime to String

Java Convert LocalDateTime to LocalDate

Java Convert LocalDateTime to LocalTime

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDateTime to Calendar

Java Convert LocalDateTime to Date

Java Convert LocalDateTime to Instant

Java Convert LocalDateTime to Epoch Seconds

Java Convert Epoch Seconds to LocalDateTime

Java Compare two LocalDateTime Values

Java Compare two LocalTime Values

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples