Java Convert Date to LocalTime

Tags: Java Date Java LocalTime Java 8

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

How to convert Date to LocalTime in Java

In Java, with a given Date object you can follow these steps to convert it to a LocalTime object.

  • Step 1: convert Date object to Instant object using the Date.toInstant() method.
  • Step 2: Use the LocalTime.ofInstant(Instant instant, ZoneId zone) method to convert the above Instant object to LocalTime object.

ConvertDateToLocalTimeExample1.java

import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

public class ConvertDateToLocalTimeExample1 {
    public static void main(String... args) {
        Date date = new Date();

        Instant instant = date.toInstant();
        ZoneId zoneId = TimeZone.getDefault().toZoneId();
        LocalTime localTime = LocalTime.ofInstant(instant, zoneId);

        System.out.println("Date: " + date);
        System.out.println("LocalTime: " + localTime);
    }
}
The output as below.
Date: Fri May 20 00:32:02 ICT 2022
LocalTime: 00:32:02.192

Happy Coding 😊

Java Convert String to LocalDateTime

Java Convert String to LocalTime

Java Convert String to LocalDate

Java Convert Date to ZonedDateTime

Java Convert Date to LocalDateTime

Java Convert Calendar to LocalDateTime

Java Convert Calendar to ZonedDateTime

Java Convert Calendar to LocalTime