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);


    }
}
-




Have Questions? Post them here!