In this article, we will learn how to convert LocalDate to GregorianCalendar using atStartOfDay() method of LocalDate provided in Java 1.8 version and vice-versa
LocalDate to GregorianCalendar conversion & vice-versa :
- There are 3 new methods introduced in Java 1.8 version for GregorianCalendar class, those are
- from(ZonedDateTime) – This static method obtains an instance of
GregorianCalendar
with the default locale from aZonedDateTime
object - toZonedDateTime() – This method converts invoking GregorianCalendar object to a
ZonedDateTime
that represents the same point on the time-line as that ofGregorianCalendar
object - getCalendarType() – Returns
"gregory"
as the calendar type
- from(ZonedDateTime) – This static method obtains an instance of
1. Convert LocalDate to GregorianCalendar :
- LocalDate consist of only Date information whereas GregorianCalendar requires Date, Time and Zone information
- GregorianCalendar = LocalDate + Time + Zone information
- For LocalDate to GregorianCalendar conversion, Time-Zone and Time information is required
- GregorianCalendar.from() method accepts ZonedDateTime as input-argument and returns GregorianCalendar
- Convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument
- Converted GregorianCalendar 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 -> GregorianCalendar
- Lets see an example for conversion of LocalDate to GregorianCalendar in the below illustration
ConvertLocalDateToGregorianCalendar.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.GregorianCalendar;
public class ConvertLocalDateToGregorianCalendar {
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 java.time.LocalDate -> java.time.ZonedDateTime -> java.util.GregorianCalendar
GregorianCalendar gregorianCalendar = GregorianCalendar.from(localDate.atStartOfDay(zoneId));
System.out.print("\nConversion of LocalDate to GregorianCalendar is :- \n"
+ gregorianCalendar.getTime());
}
}
Output:
Current System Date is :-
2022-08-02
Default System Zone is :-
Asia/Calcutta
Conversion of LocalDate to GregorianCalendar is :-
Tue Aug 02 00:00:00 IST 2022
2. Convert GregorianCalendar to LocalDate :
- Instantiate GregorianCalendar object for conversion to LocalDate
- Conversion steps –
- Convert GregorianCalendar to ZonedDateTime using toZonedDateTime() method
- And then invoke toLocalDate() method which returns LocalDate
- In short, GregorianCalendar -> ZonedDateTime -> LocalDate
- Lets see an example for conversion of GregorianCalendar to LocalDate in the below illustration
ConvertGregorianCalendarToLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.GregorianCalendar;
public class ConvertGregorianCalendarToLocalDate {
public static void main(String[] args) {
// 1. Instantiate GregorianCalendar
GregorianCalendar gregorianCalendar = new GregorianCalendar();
System.out.println("Current Date/Time is :- \n"
+ gregorianCalendar.getTime());
// 2. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault System Zone is :- \n"
+ zoneId);
// 3. convert java.util.GregorianCalendar -> java.time.ZonedDateTime -> java.time.LocalDate
LocalDate localDate = gregorianCalendar.toZonedDateTime().toLocalDate();
System.out.print("\nConversion of GregorianCalendar to LocalDate is :- \n"
+ localDate);
}
}
Output:
Current Date/Time is :-
Tue Aug 02 16:57:29 IST 2022
Default System Zone is :-
Asia/Calcutta
Conversion of GregorianCalendar 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/7/docs/api/java/util/GregorianCalendar.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 !!