Java Convert Calendar to LocalTime

Tags: Java Calendar Java LocalTime Java 8

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

How to convert Calendar to LocalTime in Java

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

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

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

public class ConvertCalendarToLocalTimeExample1 {
    public static void main(String... args) {
        Calendar calendar = Calendar.getInstance();

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

        System.out.println("Calendar hour: " + calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("Calendar minute: " + calendar.get(Calendar.MINUTE));
        System.out.println("Calendar second: " + calendar.get(Calendar.SECOND));
        System.out.println("Calendar millisecond: " + calendar.get(Calendar.MILLISECOND));
        System.out.println("LocalTime: " + localTime);
The output as below.
Calendar hour: 0
Calendar minute: 50
Calendar second: 15
Calendar millisecond: 684
LocalTime: 00:50:15.684

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 Date to LocalTime

Java Convert Calendar to LocalDateTime

Java Convert Calendar to ZonedDateTime