In this article, we will learn how to convert ZonedDateTime to Calendar using toInstant() method of ZonedDateTime provided in Java 1.8 version and vice-versa
ZonedDateTime to Calendar conversion & vice-versa :
Direct conversion from ZonedDateTime to java.util.Calendar isn’t possible and hence convert ZonedDateTime 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 ZonedDateTime to Calendar :
- Converting ZonedDateTime to Calendar is very straightforward as,
- ZoneDateTime class has toInstant() method
- Date.from() method accepts Instant as input-argument and returns java.util.Date
- For converting java.util.Date to Calendar, get an instance of Calendar using getInstance() method and then invoke setTime() method passing above converted Date object as argument
- Converted Calendar will have,
- Date & Time & Zone parts remain same as that of LocalDateTime
- In short, ZonedDateTime -> Instant -> java.util.Date -> Calendar
- Lets see an example for conversion of ZonedDateTime to Calendar in the below illustration
ConvertZonedDateTimeToJavaUtilCalendar.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
public class ConvertZonedDateTimeToJavaUtilCalendar {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n"
+ zonedDateTime);
// 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(zonedDateTime.toInstant());
// 4. convert java.util.Date to java.util.Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.print("\nConversion of ZonedDateTime to java.util.Calendar is :- \n"
+ date);
}
}
Output:
Zoned Date/time is :-
2022-08-13T16:47:30.958523400+05:30[Asia/Calcutta]
Default System Zone is :-
Asia/Calcutta
Conversion of ZonedDateTime to java.util.Calendar is :-
Sat Aug 13 16:47:30 IST 2022
2. Convert Calendar to ZonedDateTime :
- Get Calendar object using getInstance() method of Calendar for conversion to ZonedDateTime
- 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
- In short, Calendar -> java.util.Date -> Instant -> ZonedDateTime
- Lets see an example for conversion of Calendar to ZonedDateTime in the below illustration
ConvertJavaUtilCalendarToZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
public class ConvertJavaUtilCalendarToZonedDateTime {
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 -> Instant -> ZonedDateTime
Date date = calendar.getTime();
ZonedDateTime localDate = date.toInstant().atZone(zoneId);
System.out.print("\nConversion of Calendar to ZonedDateTime is :- \n"
+ localDate);
}
}
Output:
Current Date is :-
Sat Aug 13 16:47:46 IST 2022
Default System Zone is :-
Asia/Calcutta
Conversion of Calendar to ZonedDateTime is :-
2022-08-13T16:47:46.609+05:30[Asia/Calcutta]
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.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 !!