Java Convert LocalDateTime to ZonedDateTime

Tags: Java LocalDateTime Java ZonedDateTime Java 8

In this Java core tutorial we learn how to convert a java.time.LocalDateTime into java.time.ZonedDateTime object with default system time zone or a specified time zone in Java programming language.

Table of contents

  1. How to convert LocalDateTime to ZonedDateTime in Default System Time Zone
  2. How to convert LocalDateTime to ZonedDateTime in Specified Time Zone

How to convert LocalDateTime to ZonedDateTime in Default System Time Zone

With a given LocalDateTime in Java we can use atZone() method and provide a ZoneId object to convert it to ZonedDateTime object.

In the following example Java program we show how to use the LocalDateTime.atZone() method to convert a LocalDateTime to a ZonedDateTime in system default time zone.

ConvertLocalDateTimeToZonedDateTimeExample1.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ConvertLocalDateTimeToZonedDateTimeExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId systemDefaultZoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(systemDefaultZoneId);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-24T11:40:10.875265800
ZonedDateTime: 2022-04-24T11:40:10.875265800+07:00[Asia/Bangkok]

How to convert LocalDateTime to ZonedDateTime in Specified Time Zone

In the following example Java program we show how to use the LocalDateTime.atZone() method to convert a LocalDateTime to a ZonedDateTime in specified time zone Europe/Paris.

ConvertLocalDateTimeToZonedDateTimeExample2.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ConvertLocalDateTimeToZonedDateTimeExample2 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId systemDefaultZoneId = ZoneId.of("Europe/Paris");
        ZonedDateTime zonedDateTime = localDateTime.atZone(systemDefaultZoneId);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-24T11:40:42.804430100
ZonedDateTime: 2022-04-24T11:40:42.804430100+02:00[Europe/Paris]

Happy Coding 😊

Java Convert LocalDateTime to String

Java Convert LocalDateTime to LocalDate

Java Convert LocalDateTime to LocalTime

Java Convert LocalDateTime to OffsetDateTime

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

Java ZonedDateTime.now() method with Examples

Java ZonedDateTime.of() Method with Examples

Java ZonedDateTime.ofLocal() Method with Examples