Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?

In this article, we will learn how to convert LocalDate to Calendar using atStartOfDay() method of LocalDate provided in Java 1.8 version and vice-versa

1. LocalDate to Calendar conversion :

Direct conversion from java.time.LocalDate to java.util.Calendar isn’t possible and hence convert LocalDate to java.util.Date and then to Calendar using getInstance() & setTime() methods

  • There are 2 new methods introduced in Java 1.8 version for Date class, those are
    1. from(Instant) – This static method obtains an instance of Date from an Instant object
    2. toInstant() – This method converts invoking Date object to an Instant
  • Note: Many legacy methods of Date class are deprecated

1.1 Convert LocalDate to Calendar :

  • LocalDate consist of only Date information whereas Calendar requires Date, Time and Zone information
    • Calendar = LocalDate + Time + Zone information
    • For LocalDate to Calendar conversion, Time-Zone and Time information is required
  • Date.from() method accepts Instant as input-argument
    1. Convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument and then invoke toInstant() method which returns an Instant
    2. Now, pass above converted Instant from LocalDate to Date.from() method which will return java.util.Date
    3. For converting java.util.Date to Calendar, get an instance of Calendar using getInstance() method and invoke setTime() method passing above converted Date object as argument
  • Converted Calendar will have,
    1. Date part same as that of LocalDate
    2. Adding System default Zone information
    3. Time part fields hour/minute/second values set to 00
  • In short, LocalDate -> ZonedDateTime -> Instant -> java.util.Date -> Calendar
  • Lets see an example for conversion of LocalDate to Calendar in the below illustration

ConvertLocalDateToJavaUtilCalendar.java

package in.bench.resources.java8.localdate.examples;

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

public class ConvertLocalDateToJavaUtilCalendar {

	public static void main(String[] args) {

		// 1. get current System Date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" + zoneId);


		// 3. convert LocalDate to java.util.Date
		Date date = Date.from(localDate.atStartOfDay(zoneId).toInstant());


		// 4. convert java.util.Date to java.util.Calendar
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		System.out.print("\nConversion of LocalDate to java.util.Calendar is :- \n"
				+ date);
	}
}

Output:

Current System Date is :- 
2022-08-02

Default System Zone is :- 
Asia/Calcutta

Conversion of LocalDate to java.util.Calendar is :- 
Tue Aug 02 00:00:00 IST 2022

1.2 Convert Calendar to LocalDate :

  • Get Calendar object using getInstance() method of Calendar for conversion to LocalDate
  • Conversion steps
    1. Convert Calendar to Date using getTime() method
    2. And then invoke toInstant() method for Date to Instant conversion
    3. And then invoke atZone() method passing ZoneId as argument for Instant to ZonedDateTime conversion
    4. And then invoke toLocalDate() method which returns LocalDate
  • In short, Calendar -> java.util.Date -> Instant -> ZonedDateTime -> LocalDate
  • Lets see an example for conversion of Calendar to LocalDate in the below illustration

ConvertJavaUtilCalendarToLocalDate.java

package in.bench.resources.java8.localdate.examples;

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

public class ConvertJavaUtilCalendarToLocalDate {

	public static void main(String[] args) {

		// 1. get current calendar Date
		Calendar calendar = Calendar.getInstance();
		System.out.println("Current Date is :- \n" + calendar.getTime());


		// 2. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" + zoneId);


		// 3. convert java.util.Calendar -> Date -> LocalDate
		Date date = calendar.getTime();
		LocalDate localDate = date.toInstant().atZone(zoneId).toLocalDate();
		System.out.print("\nConversion of Calendar to LocalDate is :- \n"
				+ localDate);
	}
}

Output:

Current Date is :- 
Tue Aug 02 15:40:00 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of Calendar to LocalDate is :- 
2022-08-02

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?