Java Convert Instant to Calendar
Tags: Java Instant Java Calendar Java 8
In this Java core tutorial we learn how to convert a java.time.Instant object into java.util.Calendar object in Java programming language.
How to convert Instant to Calendar in Java
In Java, we can follow the steps below to create a new Calendar object from a given Instant object.
- Step 1: create a new ZonedDateTime object in system time zone from the Instant object.
- Step 2: create new Calendar object from ZonedDateTime object using GregorianCalendar.from(ZonedDateTime zdt) static method.
In the following Java code, we show how to convert Instant to Calendar.
ConvertInstantToCalendarExample1.java
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ConvertInstantToCalendarExample1 {
public static void main(String... args) {
Instant instant = Instant.now();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
Calendar calendar = GregorianCalendar.from(zonedDateTime);
System.out.println("Instant: " + instant);
System.out.println("Calendar: " + calendar);
}
}
Instant: 2022-05-17T15:39:11.196125500Z
Calendar: java.util.GregorianCalendar[time=1652801951196,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=4,WEEK_OF_YEAR=20,WEEK_OF_MONTH=3,DAY_OF_MONTH=17,DAY_OF_YEAR=137,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=39,SECOND=11,MILLISECOND=196,ZONE_OFFSET=25200000,DST_OFFSET=0]
Happy Coding 😊
Related Articles
Java Convert Instant to LocalDateTime
Java Convert Instant to LocalDate
Java Convert Instant to LocalTime
Java Convert Instant to ZonedDateTime
Java Convert Instant to OffsetDateTime
Java Convert Instant to Epoch Seconds
Java Convert Instant to Epoch Milliseconds