Java Convert Calendar to LocalDate

Tags: Java LocalDate Java Calendar Java 8

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

How to convert Calendar to LocalDate in Java

In Java to we can follow the step by step below to convert a Calendar object to LocalDate object.

  • First step, convert the Calendar object to Date object using Calendar.getTime() method.
  • Second step, convert the Date object to Instant object using Date.toInstant() method.
  • Third step, create ZonedDateTime with system default time zone from the Instant object above.
  • Finally, convert the ZonedDateTime object to LocalDate using the toLocalDate() method.

The following Java program we show how to use step by step above to convert Calendar to LocalDate.

ConvertCalendarToLocalDateExample1.java

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

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

        Date date = calendar.getTime();
        Instant instant = date.toInstant();
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        LocalDate localDate = zonedDateTime.toLocalDate();

        System.out.println("Calendar year: " + calendar.get(Calendar.YEAR));
        System.out.println("Calendar month (0 to 11): " + calendar.get(Calendar.MONTH));
        System.out.println("Calendar day of month: " + calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("LocalDate: " + localDate);
    }
}
The output as below.
Calendar year: 2022
Calendar month (0 to 11): 3
Calendar day of month: 18
LocalDate: 2022-04-18

Or we can write Java code in shorter version with one line of code as below.

LocalDate localDate = calendar.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

In the following Java program we use line of code above to convert Calendar to LocalDate.

ConvertCalendarToLocalDateExample2.java

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

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

        LocalDate localDate = calendar.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        System.out.println("Calendar year: " + calendar.get(Calendar.YEAR));
        System.out.println("Calendar month (0 to 11): " + calendar.get(Calendar.MONTH));
        System.out.println("Calendar day of month: " + calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("LocalDate: " + localDate);
    }
}
The output as below.
Calendar year: 2022
Calendar month (0 to 11): 3
Calendar day of month: 18
LocalDate: 2022-04-18

Happy Coding 😊

Java LocalDate by Examples

Java Convert LocalDate to Calendar

Java Convert LocalDate to String

Java Convert LocalDate to LocalDateTime

Java Convert LocalDate to OffsetDateTime

Java Convert LocalDate to Epoch Day

Java Convert LocalDate to ZonedDateTime

Java Convert LocalDate to Date

Java Convert Date to LocalDate

Java Convert Epoch Day to LocalDate