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
LocalDate to Calendar conversion & vice-versa :
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
- from(Instant) – This static method obtains an instance of
Date
from anInstant
object - toInstant() – This method converts invoking
Date
object to anInstant
- from(Instant) – This static method obtains an instance of
- Note: Many legacy methods of Date class are deprecated
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
- Convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument and then invoke toInstant() method which returns an Instant
- Now, pass above converted Instant from LocalDate to Date.from() method which will return java.util.Date
- 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,
- Date part same as that of LocalDate
- Adding System default Zone information
- 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
2. Convert Calendar to LocalDate :
- Get Calendar object using getInstance() method of Calendar for conversion to LocalDate
- Conversion steps –
- Convert Calendar to Date using getTime() method
- And then invoke toInstant() method for Date to Instant conversion
- And then invoke atZone() method passing ZoneId as argument for Instant to ZonedDateTime conversion
- 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:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!