In this article, we will learn how to convert Instant to Calendar and vice-versa using ZonedDateTime & Date classes in Java 1.8 version
Instant to Calendar conversion & vice-versa :
Direct conversion from Instant to java.util.Calendar isn’t possible and hence first convert Instant to ZonedDateTime/Date and then to Calendar whereas for reverse conversion Calendar class has introduced new method called toInstant() which directly converts Calendar to an Instant
1. Convert Instant to Calendar :
Convert Instant to either ZonedDateTime/Date and then to Calendar,
- Convert Instant to Calendar via ZonedDateTime
- Convert Instant to Calendar via Date
1.1 Convert Instant to Calendar via ZonedDateTime :
- First, convert Instant to ZonedDateTime using ZonedDateTime.ofInstant() method passing instant & ZoneOffset as input–arguments
- Pass converted ZonedDateTime as input–argument to GregorianCalendar.from() method which returns Calendar
- Converted Calendar will have,
- Date & Time & Zone/Offset parts corresponding to that particular default system Zone
- In short, Instant -> java.util.Date -> Calendar
- Lets see an example for conversion of Instant to Calendar in the below illustration
ConvertInstantToJavaUtilCalendar1.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ConvertInstantToJavaUtilCalendar1 {
public static void main(String[] args) {
// get instantaneous moment at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n"
+ instant);
// first, convert Instant to ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime
.ofInstant(instant, ZoneOffset.of("+05:30"));
System.out.println("\nZonedDateTime is :- \n" + zonedDateTime);
// Instant -> ZonedDateTime -> GregorianCalendar -> Calendar
Calendar calendar = GregorianCalendar.from(zonedDateTime);
System.out.println("\nConversion of Instant to Calendar is :- \n"
+ calendar);
// print Time
System.out.print("\nCurrent Date/time is :- \n"
+ calendar.getTime());
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-21T05:30:45.665061800Z
ZonedDateTime is :-
2022-08-21T11:00:45.665061800+05:30
Conversion of Instant to Calendar is :-
java.util.GregorianCalendar[time=1661059845665,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,
dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,
minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=33,
WEEK_OF_MONTH=3,DAY_OF_MONTH=21,DAY_OF_YEAR=233,DAY_OF_WEEK=1,
DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,
MINUTE=0,SECOND=45,MILLISECOND=665,ZONE_OFFSET=19800000,DST_OFFSET=0]
Current Date/time is :-
Sun Aug 21 11:00:45 IST 2022
1.2 Convert Instant to Calendar via Date :
- Conversion steps for Instant to Calendar –
- 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 input–argument
- Converted Calendar will have,
- Date & Time & Zone/Offset parts corresponding to that particular default system Zone
- In short, Instant -> java.util.Date -> Calendar
- Lets see an example for conversion of Instant to Calendar in the below illustration
ConvertInstantToJavaUtilCalendar2.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
public class ConvertInstantToJavaUtilCalendar2 {
public static void main(String[] args) {
// get instantaneous moment at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n"
+ instant);
// first, convert Instant to Date
Date date = Date.from(instant);
System.out.println("\nConversion of Instant to Date is :- \n"
+ date);
// Instant -> Date -> Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("\nConversion of Instant to Calendar is :- \n"
+ calendar);
// print Time
System.out.print("\nCurrent Date/time is :- \n"
+ calendar.getTime());
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-21T05:29:58.407601600Z
Conversion of Instant to Date is :-
Sun Aug 21 10:59:58 IST 2022
Conversion of Instant to Calendar is :-
java.util.GregorianCalendar[time=1661059798407,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=35,
WEEK_OF_MONTH=4,DAY_OF_MONTH=21,DAY_OF_YEAR=233,DAY_OF_WEEK=1,
DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,
MINUTE=59,SECOND=58,MILLISECOND=407,ZONE_OFFSET=19800000,DST_OFFSET=0]
Current Date/time is :-
Sun Aug 21 10:59:58 IST 2022
2. Convert Calendar to an Instant :
- Get Calendar object using getInstance() method of Calendar for conversion to an Instant
- Conversion steps –
- Invoke toInstant() method of Calendar for Calendar to Instant conversion
- In short, Calendar -> Instant
- Lets see an example for conversion of Calendar to an Instant in the below illustration
ConvertJavaUtilCalendarToInstant.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.util.Calendar;
public class ConvertJavaUtilCalendarToInstant {
public static void main(String[] args) {
// 1. get current calendar Date
Calendar calendar = Calendar.getInstance();
System.out.println("Current Date is :- \n"
+ calendar);
// print time
System.out.println("\nCurrent Date/Time is :- \n"
+ calendar.getTime());
// 2. convert Calendar to Instant
Instant instant = calendar.toInstant();
System.out.print("\nConversion of Calendar to an Instant is :- \n"
+ instant);
}
}
Output:
Current Date is :-
java.util.GregorianCalendar[time=1661059763675,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=35,
WEEK_OF_MONTH=4,DAY_OF_MONTH=21,DAY_OF_YEAR=233,DAY_OF_WEEK=1,
DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,
MINUTE=59,SECOND=23,MILLISECOND=675,ZONE_OFFSET=19800000,DST_OFFSET=0]
Current Date/Time is :-
Sun Aug 21 10:59:23 IST 2022
Conversion of Calendar to an Instant is :-
2022-08-21T05:29:23.675Z
Related Articles:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.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/ZoneOffset.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/sql/Timestamp.html
Happy Coding !!
Happy Learning !!