Java Convert Date to LocalDate

Tags: Java LocalDate Java Date Java 8

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

How to convert Date to LocalDate in Java

In Java we can follow the steps below to convert a Date object to LocalDate object.

  • First step, convert Date object to Instant object.
  • Second step, Convert Instant object to ZonedDateTime with system default time zone.
  • Finally convert the ZonedDateTime to LocalDate using the toLocalDate() method.

The following Java program to show how to use the steps above to convert Date to LocalDate.

ConvertDateToLocalDateExample1.java

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

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

        Instant instant = date.toInstant();
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        LocalDate localDate = zonedDateTime.toLocalDate();

        System.out.println("Date: " + date);
        System.out.println("LocalDate: " + localDate);
    }
}
The output as below.
Date: Mon Apr 18 21:22:28 ICT 2022
LocalDate: 2022-04-18

We also can write it in one line of code as below.

LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

The following Java program to use the line of code above to convert Date to LocalDate.

ConvertDateToLocalDateExample2.java

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

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

        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        System.out.println("Date: " + date);
        System.out.println("LocalDate: " + localDate);
    }
}
The output as below.
Date: Mon Apr 18 21:22:50 ICT 2022
LocalDate: 2022-04-18

Happy Coding 😊

Java LocalDate by Examples

Java Convert LocalDate to Date

Java Convert LocalDate to String

Java Convert LocalDate to LocalDateTime

Java Convert LocalDate to OffsetDateTime

Java Convert LocalDate to Epoch Day

Java Convert LocalDate to ZonedDateTime

Java Convert LocalDate to Calendar

Java Convert Calendar to LocalDate

Java Convert Epoch Day to LocalDate