-->
Skip to main content

Convert LocalDate to Date in Java [2021]

Convert LocalDate to Date in Java [2021] 

To convert LocalDate to date in Java, Date class added one method public static Date from(Instant instant) from 1.8 onwards. so to Convert our LocalDate instance to Date we have to first convert LocalDate instance to Instant class object then only we can get Date from LocalDate. In the recent tutorial, we have also shared Date to LocalDate Java Conversion

LocalDate to Date In Java

  1.  In the first step, we will create a ZonedDateTime instance using a LocalDate object.
  2.  In the second step, we will create Instant instance using ZonedDateTime instance with  System default ZoneId.
  3.  In the Third Step, we will create Date instance using Instant instance created in step 2.

In this tutorial, we will see how we can convert LocalDate to Date in Java

LocalDate to Date

Here is the brief information regarding the method

public static Date from(Instant instant)

  • This method gets the instance of the Date from an Instant object.

Parameters:

instant - the instant to convert to date.

Returns:

a Date representing the same point on the time-line as the provided instant.

Throws:

  • NullPointerException is thrown if instant is null.
  • IllegalArgumentException is thrown if the instant is too large to represent as a Date.

Since:

This method is added in Java version 1.8.

Example:

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

 public class LocalDateToDateExample1 {
  public static void main(String[] args) {
   //Creating LocalDate instance
   LocalDate now = LocalDate.now();
   //Creating ZonedDateTime instance
   ZonedDateTime zdt = now.atStartOfDay(ZoneId.systemDefault());
   //Creating Instant instance
   Instant instant = zdt.toInstant();
   //Creating Date instance using instant instance.
   Date date = Date.from(instant);
   System.out.println("LocalDate To Date :  " + date);
  }
 }

Output:

LocalDate To Date :  Sun Jul 05 00:00:00 IST 2020

Conclusion:

In this tutorial, we have seen how we can convert LocalDate to Date in Java using public static Date from(Instant instant) of Date class.

Comments