Java Convert Date to ZonedDateTime

Tags: Java Date Java ZonedDateTime Java 8

In this Java core tutorial we learn how to convert a java.util.Date object to a java.time.ZonedDateTime object in Java programming language.

How to convert Date to ZonedDateTime in Java

In Java, with a given Date object you can follow these steps to convert it to a ZonedDateTime object.

  • Step 1: convert Date object to Instant object using the Date.toInstant() method.
  • Step 2: Use the ZonedDateTime.ofInstant(Instant instant, ZoneId zone) method to convert the above Instant object to ZonedDateTime object in system default time zone.

ConvertDateToZonedDateTimeExample1.java

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;

public class ConvertDateToZonedDateTimeExample1 {
    public static void main(String... args) {
        Date date = new Date();

        Instant instant = date.toInstant();
        ZoneId zoneId = TimeZone.getDefault().toZoneId();
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);

        System.out.println("Date: " + date);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
Date: Fri May 20 00:21:24 ICT 2022
ZonedDateTime: 2022-05-20T00:21:24.936+07:00[Asia/Bangkok]

Happy Coding 😊

Java Convert String to LocalDateTime

Java Convert String to LocalTime

Java Convert String to LocalDate

Java Convert Date to LocalDateTime

Java Convert Date to LocalTime

Java Convert Calendar to LocalDateTime

Java Convert Calendar to ZonedDateTime

Java Convert Calendar to LocalTime