Java 8 – How to convert LocalDate to an Instant ?

In this article, we will learn how to convert LocalDate to Instant using atStartOfDay() method of LocalDate provided in Java 1.8 version

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

Convert LocalDate to an Instant:

  • First, convert LocalDate to either ZonedDateTime/LocalDateTime using variants of atStartOfDay() method and then to an Instant using toInstant() method
  • 1st variant –
    • atStartOfDay(ZoneId) – this method takes ZoneId as argument and returns ZonedDateTime
    • After this conversion, convert ZonedDateTime to an Instant using toInstant() method of ZonedDateTime (inherited from ChronoZonedDateTime)
  • 2nd variant –
    • atStartOfDay() – this method takes no argument and returns LocalDateTime
    • After this conversion, convert LocalDateTime to an Instant using toInstant(ZoneOffset) method of LocalDateTime (inherited from ChronoLocalDateTime)
  • Lets see an example for both conversion of LocalDate to an Instant

1. Convert LocalDate to an Instant via ZonedDateTime :

  • First, convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument and then invoke toInstant() method which returns Instant
    1. 1st conversion LocalDate to ZonedDateTime returns 20220801T00:00+05:30[Asia/Calcutta]
    2. 2nd conversion ZonedDateTime to an Instant returns 20220731T18:30:00Z
  • In short, LocalDate -> ZonedDateTime -> Instant
  • Note: Instant always provide instantaneous moment at UTC/GMT in yyyy-MM-ddTHH:mm:ss.nnnZ format

ConvertLocalDateToInstant.java

package in.bench.resources.java8.localdate.examples;

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

public class ConvertLocalDateToInstant {

	public static void main(String[] args) {

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


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


		// 3. convert LocalDate to Instant
		Instant instant = localDate.atStartOfDay(zoneId).toInstant();
		System.out.print("\nConversion of LocalDate to Instant with ZoneId is :- \n" 
				+ instant);
	}
}

Output:

Current System Date is :- 
2022-08-01

System default Zone is :- 
Asia/Calcutta

Conversion of LocalDate to Instant with ZoneId is :- 
2022-07-31T18:30:00Z

2. Convert LocalDate to an Instant via LocalDateTime :

  • First, convert LocalDate to LocalDateTime using atStartOfDay() method and then invoke toInstant() method passing ZoneOffset as argument which returns Instant
    1. 1st conversion LocalDate to LocalDateTime returns 20220801T00:00
    2. 2nd conversion LocalDateTime to an Instant returns 20220801T00:00:00Z
  • In short, LocalDate -> LocalDateTime -> Instant
  • Note: Instant always provide instantaneous moment at UTC/GMT in yyyy-MM-ddTHH:mm:ss.nnnZ format

ConvertLocalDateToInstant2.java

package in.bench.resources.java8.localdate.examples;

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

public class ConvertLocalDateToInstant2 {

	public static void main(String[] args) {

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


		// 2. get UTC ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.UTC;
		System.out.println("\nUTC ZoneOffset is :- \n" + zoneOffset);


		// 3. convert LocalDate to Instant
		Instant instant = localDate.atStartOfDay().toInstant(zoneOffset);
		System.out.print("\nConversion of LocalDate to Instant with UTC ZoneOffset is :- \n" 
				+ instant);
	}
}

Output:

Current System Date is :- 
2022-08-01

UTC ZoneOffset is :- 
Z

Conversion of LocalDate to Instant with UTC ZoneOffset is :- 
2022-08-01T00:00:00Z

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
Java 8 – How to convert LocalDate to an OffsetDateTime ?