Java Convert LocalDate to Calendar
Tags: Java LocalDate Java Calendar Java 8
In this Java core tutorial we learn how to convert java.time.LocalDate object to java.util.Calendar object in Java programming language.
How to convert LocalDate to Calendar in Java
In Java to convert a LocalDate object to Calendar we can follow the steps below
- First step, Convert LocalDate to a ZonedDateTime
- Second step, Convert ZonedDateTime to Instant object
- Third step, Convert Instant to Date object
- Then create new instance of Calendar and use Calendar.setTime() method to set Date object to Calendar.
The following Java example to show how to convert LocalDate to Calendar as above step by step.
ConvertLocalDateToCalendarExample1.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 ConvertLocalDateToCalendarExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 7, 11);
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
Instant instant = zonedDateTime.toInstant();
Date date = Date.from(instant);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("LocalDate: " + localDate);
System.out.println("Calendar year: " + calendar.get(Calendar.YEAR));
System.out.println("Calendar month (0 to 11): " + calendar.get(Calendar.MONTH));// Calendar mont from 0 to 11
System.out.println("Calendar day of month: " + calendar.get(Calendar.DAY_OF_MONTH));
}
}
LocalDate: 2022-07-11
Calendar year: 2022
Calendar month (0 to 11): 6
Calendar day of month: 11
Or we can use shorter version as below.
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
ConvertLocalDateToCalendarExample2.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 ConvertLocalDateToCalendarExample2 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 7, 11);
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("LocalDate: " + localDate);
System.out.println("Calendar year: " + calendar.get(Calendar.YEAR));
System.out.println("Calendar month (0 to 11): " + calendar.get(Calendar.MONTH));// Calendar mont from 0 to 11
System.out.println("Calendar day of month: " + calendar.get(Calendar.DAY_OF_MONTH));
}
}
LocalDate: 2022-07-11
Calendar year: 2022
Calendar month (0 to 11): 6
Calendar day of month: 11
Happy Coding 😊
Related Articles
Java Convert Calendar to LocalDate
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