Java 9 – How to convert Instant to LocalDate using ofInstant() method ?

In this article, we will learn how to convert an Instant to LocalDate using newly introduced method in Java 1.9 version

1. Convert Instant to LocalDate in Java 1.9 version :

  • Direct conversion between LocalDate and Instant isn’t possible till Java 1.8 version
  • But in Java 1.9 version a new method LocalDate.ofInstant() introduced for direct conversion from Instant to LocalDate
    • LocalDate.ofInstant​(Instant, ZoneId) – gets an instance of LocalDate from an Instant and zone ID passed

ConvertInstantToLocalDate.java

package in.bench.resources.java9.conversion;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;

public class ConvertInstantToLocalDate {

	public static void main(String[] args) {

		// 1. get Instant
		Instant instant = Instant.now();
		System.out.println("Current Instant is :- \n" 
				+ instant);


		// 2. get system default zone offset
		ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
		System.out.println("\nZone Offset is :- \n" 
				+ zoneOffset);


		// 3. convert Instant to LocalDate using ofInstant() method
		LocalDate localDate = LocalDate.ofInstant(instant, zoneOffset);
		System.out.print("\nConversion of Instant to LocalDate :- \n" 
				+ localDate);
	}
}

Output:

Current Instant is :- 
2022-08-23T11:26:43.566584600Z

Zone Offset is :- 
+05:30

Conversion of Instant to LocalDate :- 
2022-08-23

2. Convert LocalDate to an Instant :

Direct conversion between LocalDate & Instant isn’t possible therefore convert LocalDate to either LocalDateTime/ZonedDateTime/OffsetDateTime and then use/invoke atStartOfDay() or toInstant() method of respective classes to convert to an Instant

  • First step is to convert LocalDate to ZonedDateTime using atStartOfDay() method of LocalDate class passing ZoneId as inputargument
  • After converting to ZonedDateTime, use/invoke toInstant() method of ZonedDateTime to convert into an Instant as shown in the below illustration

ConvertLocalDateToInstant.java

package in.bench.resources.java9.conversion;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ConvertLocalDateToInstant {

	public static void main(String[] args) {

		// 1. get LocalDate
		LocalDate localDate = LocalDate.now();
		System.out.println("Current LocalDate is :- \n" 
				+ localDate);


		// 2. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault Zone Id is :- \n" 
				+ zoneId);


		// 3. convert LocalDate to ZonedDateTime
		ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
		System.out.println("\nConversion of LocalDate to ZonedDateTime :- \n" 
				+ zonedDateTime);


		// 4. convert LocalDate -> ZonedDateTime -> Instant
		Instant instant = zonedDateTime.toInstant();
		System.out.print("\nConversion of LocalDate -> ZonedDateTime -> Instant :- \n" 
				+ instant);
	}
}

Output:

Current LocalDate is :- 
2022-08-23

Default Zone Id is :- 
Asia/Calcutta

Conversion of LocalDate to ZonedDateTime :- 
2022-08-23T00:00+05:30[Asia/Calcutta]

Conversion of LocalDate -> ZonedDateTime -> Instant :- 
2022-08-22T18:30:00Z

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalTime to ZonedDateTime ?
Java 9 – Find difference between two Instant instances upto nanosecond precision ?