Java Convert LocalTime to Calendar

Tags: Java LocalTime Java Calendar Java 8

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

How to convert LocalTime to Calendar

In order to convert a LocalTime object to a Calendar object we can follow the steps below.

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

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

ConvertLocalTimeToCalendarExample1.java

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

public class ConvertLocalTimeToCalendarExample1 {
    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);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        System.out.println("LocalTime: " + localTime);
        System.out.println("Calendar Hour: " + calendar.get(Calendar.HOUR));
        System.out.println("Calendar Minute: " + calendar.get(Calendar.MINUTE));
        System.out.println("Calendar Second: " + calendar.get(Calendar.SECOND));
    }
}
The output as below.
LocalTime: 10:20:30
Calendar Hour: 10
Calendar Minute: 20
Calendar Second: 30

Below is the shorter Java code version which return the same result.

ConvertLocalTimeToCalendarExample2.java

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

public class ConvertLocalTimeToCalendarExample2 {
    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());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        System.out.println("LocalTime: " + localTime);
        System.out.println("Calendar Hour: " + calendar.get(Calendar.HOUR));
        System.out.println("Calendar Minute: " + calendar.get(Calendar.MINUTE));
        System.out.println("Calendar Second: " + calendar.get(Calendar.SECOND));
    }
}
The output as below.
LocalTime: 10:20:30
Calendar Hour: 10
Calendar Minute: 20
Calendar Second: 30

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 Date

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