Java Convert Date to LocalDate in UTC
Tags: Java Date Java LocalDate Java 8 UTC
In this Java core tutorial we learn how to convert a java.util.Date object to a java.time.LocalDate object in UTC time zone offset.
How to convert Date to UTC LocalDate in Java
In Java, with a given Date object we can follow these steps to convert it to an UTC LocalDate 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.toLocalDate() method to convert the ZonedDateTime object of step 2 to a LocalDate object.
ConvertDateToUTCLocalDateExample1.java
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class ConvertDateToUTCLocalDateExample1 {
public static void main(String... args) {
Date date = new Date();
// Convert Date object to UTC LocalDate object
Instant instant = date.toInstant();
ZoneId utcZoneId = ZoneId.of("Z");
ZonedDateTime zonedDateTime = instant.atZone(utcZoneId);
LocalDate localDate = zonedDateTime.toLocalDate();
System.out.println("Date: " + date);
System.out.println("UTC LocalDate: " + localDate);
}
}
Date: Tue May 24 21:23:03 ICT 2022
UTC LocalDate: 2022-05-24
Happy Coding 😊
Related Articles
Java Convert LocalDate to Date in UTC
Java Convert LocalDateTime to Date in UTC
Java Convert Date to LocalDateTime in UTC