Java Convert Date to LocalDateTime in UTC

Tags: Java Date Java LocalDateTime 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.
  • Step 3: use the ZonedDateTime.toLocalDateTime() method to convert the ZonedDateTime object of step 2 to a LocalDateTime object.

ConvertDateToUTCLocalDateTimeExample1.java

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

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

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

        System.out.println("Date: " + date);
        System.out.println("UTC LocalDateTime: " + localDateTime);
    }
}
The output as below.
Date: Tue May 24 21:34:56 ICT 2022
UTC LocalDateTime: 2022-05-24T14:34:56.619

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 ZonedDateTime in UTC

Java Convert Date to OffsetDateTime in UTC