Java Convert LocalDateTime to Calendar

Tags: Java LocalDateTime Java Calendar Java 8

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

How to convert LocalDateTime to Calendar in Java

In Java to convert a LocalDateTime object to Calendar we can follow the steps below.

  • First step, convert the LocalDateTime to Instant object.
  • Second step, create new Date object from the above Instant.
  • Finally create new instance of Calendar and use setTime() method to set Date value.

ConvertLocalDateTimeToCalendarExample1.java

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

public class ConvertLocalDateTimeToCalendarExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("Calendar: " + calendar);
    }
}
The output as below.
LocalDateTime: 2022-04-24T13:20:43.148304
Calendar: java.util.GregorianCalendar[time=1650781243148,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=3,WEEK_OF_YEAR=18,WEEK_OF_MONTH=5,DAY_OF_MONTH=24,DAY_OF_YEAR=114,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=20,SECOND=43,MILLISECOND=148,ZONE_OFFSET=25200000,DST_OFFSET=0]

Happy Coding 😊

Java Convert LocalDateTime to String

Java Convert LocalDateTime to LocalDate

Java Convert LocalDateTime to LocalTime

Java Convert LocalDateTime to OffsetDateTime

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDateTime to Date

Java Convert LocalDateTime to Instant

Java Convert LocalDateTime to Epoch Seconds

Java Convert Epoch Seconds to LocalDateTime

Java Compare two LocalDateTime Values

Java Compare two LocalTime Values

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