Java Convert Date to ZonedDateTime in UTC

Tags: Java Date Java ZonedDateTime Java 8 UTC

In this Java core tutorial we learn how to convert a java.util.Date object to a java.time.LocalDateTime object in UTC time zone offset.

How to convert Date to UTC LocalDateTime in Java

In Java, with a given Date object we can follow these steps to convert it to an UTC LocalDateTime object.

  • Step 1: use the Date.toInstant() method to convert the Date object to an Instant object.
  • Step 2: use the Instant.atZone(ZoneId zone) method to convert the Instant object of step 1 to a ZonedDateTime object in UTC time zone.

ConvertDateToUTCZonedDateTimeExample1.java

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

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

        // Convert Date object to UTC ZonedDateTime object
        Instant instant = date.toInstant();
        ZoneId utcZoneId = ZoneId.of("Z");
        ZonedDateTime zonedDateTime = instant.atZone(utcZoneId);

        System.out.println("Date: " + date);
        System.out.println("UTC ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
Date: Tue May 24 21:38:11 ICT 2022
UTC ZonedDateTime: 2022-05-24T14:38:11.408Z

Happy Coding 😊

Java Convert LocalDate to Date in UTC

Java Convert LocalDateTime to Date in UTC

Java Convert Date to LocalDate in UTC

Java Convert Date to LocalDateTime in UTC

Java Convert Date to OffsetDateTime in UTC