Java: Convert LocalDate to java.util.Date


Step 1: Let's create a LocalDate object to represent the current date.

LocalDate localDate = LocalDate.now();

Step 2: In order to convert LocalDate to Date, we first need to covert the LocalDate to an Instant class object.

Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); 

Step 3: Now we can convert the instant object to java.util.Date class object.

Date date = Date.from(instant);


Complete Java Code

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

public class LocalDateToDateExample {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);

        System.out.println(date);

    }
}

Fri Apr 21 00:00:00 UTC 2023

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap