Java 8 – How to convert Instant to ZonedDateTime ?

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

  1. Convert Instant to ZonedDateTime using atZone() method
  2. 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 inputargument

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 inputarguments

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 :

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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Instant to an OffsetDateTime ?
Java 8 – How to convert Instant to LocalDateTime ?