Convert java.time.LocalTime to java.util.Date Object


Step 1: Get the current local time

LocalTime localTime = LocalTime.now();

Step 2: Get the current local date

LocalDate currentDate = LocalDate.now();

Step 3: Combine the current date and local time into a LocalDateTime object.

LocalDateTime localDateTime = localTime.atDate(currentDate);

Step 4: Get the time zone of the system

ZoneId systemTimeZone = ZoneId.systemDefault();

Step 5: Now convert the LocalDateTime object to a ZonedDateTime object.

ZonedDateTime zonedDateTime = localDateTime.atZone(systemTimeZone);

Step 6: Convert ZonedDateTime object to an Instant object

Instant instant = zonedDateTime.toInstant();

Step 7: Finally convert the Instant object to a Date object

Date date = Date.from(instant);

Complete Code

import java.time.*;
import java.util.Date;

public class LocalDateTimeToCalendarExample {

    public static void main(String[] args) {

        LocalTime localTime = LocalTime.now();
        LocalDate currentDate = LocalDate.now();
        LocalDateTime localDateTime = localTime.atDate(currentDate);
        ZoneId systemTimeZone = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(systemTimeZone);
        Instant instant = zonedDateTime.toInstant();
        Date date = Date.from(instant);


    }
}
-

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


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. 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