Java 8 – How to convert ZonedDateTime to an Instant ?

In this article, we will learn how to convert ZonedDateTime to an Instant using toInstant() method of ZonedDateTime/ChronoZonedDateTime provided in Java 1.8 version

For Instant to ZonedDateTime conversion, read Java 8 – How to convert Instant to ZonedDateTime ?

Convert ZonedDateTime to an Instant :

  • ZonedDateTime has a method toInstant() which returns Instant
    • toInstant() – Converts invoking ZonedDateTime/ChronoZonedDateTime to an Instant
  • Using this method, it is very easy to convert ZonedDateTime to an Instant
  • After conversion, Instant will represent instantaneous/current moment at GMT/UTC
  • Lets see an example for conversion of ZonedDateTime to an Instant in the below illustration

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-13T15:23:42.875564600+05:30[Asia/Calcutta]

Zone is :- 
Asia/Calcutta

Conversion of ZonedDateTime to an Instant is :- 
2022-08-13T09:53:42.875564600Z

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?