In this article, we will learn different ways to convert an Instant to ZonedDateTime using different methods provided in Java 1.8 version
For ZonedDateTime to an Instant conversion, read Java 8 – How to convert ZonedDateTime to an Instant ?
1. Convert Instant to ZonedDateTime :
Both Instant & ZonedDateTime classes has methods to convert Instant to ZonedDateTime directly using atZone() & ofInstant() methods
- Convert Instant to ZonedDateTime using atZone() method
- Convert Instant to ZonedDateTime using ofInstant() method
1.1 Convert Instant to ZonedDateTime using atZone() method :
- Convert Instant to ZonedDateTime using Instant.atZone() method passing ZoneId as input–argument
ConvertInstantToZonedDateTime1.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ConvertInstantToZonedDateTime1 {
public static void main(String[] args) {
// 1. get instantaneous moment at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n" + instant);
// 2. convert Instant to ZonedDateTime
ZonedDateTime zonedDateTime = instant
.atZone(ZoneId.systemDefault());
System.out.print("\nZonedDateTime is :- \n" + zonedDateTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:40:41.408268800Z
ZonedDateTime is :-
2022-08-19T22:10:41.408268800+05:30[Asia/Calcutta]
1.2 Convert Instant to ZonedDateTime using ofInstant() method :
- Convert Instant to ZonedDateTime using ZonedDateTime.ofInstant() method passing instant & ZoneOffset as input–arguments
ConvertInstantToZonedDateTime2.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ConvertInstantToZonedDateTime2 {
public static void main(String[] args) {
// 1. get instantaneous moment at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n" + instant);
// 2. convert Instant to ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime
.ofInstant(instant, ZoneOffset.of("+05:30"));
System.out.print("\nZonedDateTime is :- \n" + zonedDateTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:40:52.095227900Z
ZonedDateTime is :-
2022-08-19T22:10:52.095227900+05:30
2. Convert ZonedDateTime to an Instant :
- Convert ZonedDateTime to an Instant using toInstant() method of ZonedDateTime
- Read Java 8 – How to convert ZonedDateTime to an Instant ? for more details and examples
ConvertZonedDateTimeToInstant.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.Instant;
import java.time.ZonedDateTime;
public class ConvertZonedDateTimeToInstant {
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 Zone
System.out.println("\nZone is :- \n"
+ zonedDateTime.getZone());
// 3. convert ZonedDateTime to Instant using toOffsetDateTime()
Instant instant = zonedDateTime.toInstant();
System.out.print("\nConversion of ZonedDateTime to an Instant is :- \n"
+ instant);
}
}
Output:
Zoned Date/time is :-
2022-08-19T22:10:27.642809300+05:30[Asia/Calcutta]
Zone is :-
Asia/Calcutta
Conversion of ZonedDateTime to an Instant is :-
2022-08-19T16:40:27.642809300Z
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/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.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
Happy Coding !!
Happy Learning !!