Java Convert LocalTime to Date

Tags: Java LocalTime Java Date Java 8

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

How to convert LocalTime to Date

In order to convert a LocalTime object to a Date object we can follow these steps below.

  • Combine LocalTime with current system date as a LocalDate object to create new LocalDateTime object.
  • Combine LocalDateTime with system default time zone to create a new Instant object.
  • From an Instant we can create new Date object using Date.from() method.

The following Java code to show how to implement the steps above in Java program.

ConvertLocalTimeToDateExample1.java

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

public class ConvertLocalTimeToDateExample1 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(10, 20, 30);

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

        System.out.println("LocalTime: " + localTime);
        System.out.println("Date: " + date);
    }
}
The output as below.
LocalTime: 10:20:30
Date: Fri Apr 22 10:20:30 ICT 2022

Or we can make it shorter with one line of code as below to convert LocalTime to Date.

Date date = Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant());

ConvertLocalTimeToDateExample2.java

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

public class ConvertLocalTimeToDateExample2 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(10, 20, 30);

        Date date = Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant());

        System.out.println("LocalTime: " + localTime);
        System.out.println("Date: " + date);
    }
}
The output as below.
LocalTime: 10:20:30
Date: Fri Apr 22 10:20:30 ICT 2022

Happy Coding 😊

Java Convert LocalTime to String

Java Convert LocalTime to Seconds of Day

Java Convert LocalTime to Nanos of Day

Java Convert LocalTime to Epoch Seconds

Java Convert LocalTime to LocalDateTime

Java Convert LocalTime to OffsetTime

Java Convert LocalTime to Calendar

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples