Java 8 – How to convert Instant to LocalDate ?

In this article, we will learn different ways to convert an Instant to LocalDate using different methods provided in Java 1.8 version

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

1. Convert Instant to LocalDate :

Direct conversion from Instant to LocalDate isn’t possible and hence convert Instant to either LocalDateTime/ZonedDateTime/OffsetDateTime and then use toLocalDate() method of respective classes to convert to LocalDate

  1. Convert Instant to LocalDate via LocalDateTime
  2. Convert Instant to LocalDate via ZonedDateTime
  3. Convert Instant to LocalDate via OffsetDateTime

1.1 Convert Instant to LocalDate via LocalDateTime :

  • First step is to convert Instant to LocalDateTime using LocalDateTime.ofInstant() method passing instant & ZoneOffset
  • After converting to LocalDateTime, use/invoke toLocalDate() method of LocalDateTime to convert into LocalDate as shown in the below illustration

ConvertInstantToLocalDate1.java

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

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

public class ConvertInstantToLocalDate1 {

	public static void main(String[] args) {

		// get instantaneous moment at UTC/GMT
		Instant instant = Instant.now();
		System.out.println("Current Instant at UTC/GMT is :- \n" + instant);


		// 1. convert Instant to LocalDateTime
		LocalDateTime localDateTime = LocalDateTime
				.ofInstant(instant, ZoneOffset.UTC);
		System.out.println("\nLocalDateTime is :- \n" + localDateTime);


		// 2. Instant -> LocalDateTime -> LocalDate
		LocalDate localDate = localDateTime.toLocalDate();
		System.out.print("\nLocalDate is :- \n" + localDate);
	}
}

Output:

Current Instant at UTC/GMT is :- 
2022-08-19T15:12:25.933590Z

LocalDateTime is :- 
2022-08-19T15:12:25.933590

LocalDate is :- 
2022-08-19

1.2 Convert Instant to LocalDate via ZonedDateTime :

  • First step is to convert Instant to ZonedDateTime using ZonedDateTime.ofInstant() method passing instant & ZoneOffset
  • After converting to ZonedDateTime, use/invoke toLocalDate() method of ZonedDateTime to convert into LocalDate as shown in the below illustration

ConvertInstantToLocalDate2.java

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

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

public class ConvertInstantToLocalDate2 {

	public static void main(String[] args) {

		// get instantaneous moment at UTC/GMT
		Instant instant = Instant.now();
		System.out.println("Current Instant at UTC/GMT is :- \n" + instant);


		// 1. convert Instant to ZonedDateTime
		ZonedDateTime zonedDateTime = ZonedDateTime
				.ofInstant(instant, ZoneOffset.UTC);
		System.out.println("\nZonedDateTime is :- \n" + zonedDateTime);


		// 2. Instant -> ZonedDateTime -> LocalDate
		LocalDate localDate = zonedDateTime.toLocalDate();
		System.out.print("\nLocalDate is :- \n" + localDate);
	}
}

Output:

Current Instant at UTC/GMT is :- 
2022-08-19T15:12:36.683002600Z

ZonedDateTime is :- 
2022-08-19T15:12:36.683002600Z

LocalDate is :- 
2022-08-19

1.3 Convert Instant to LocalDate via OffsetDateTime :

  • First step is to convert Instant to an OffsetDateTime using OffsetDateTime.ofInstant() method passing instant & ZoneOffset
  • After converting to an OffsetDateTime, use/invoke toLocalDate() method of OffsetDateTime to convert into LocalDate as shown in the below illustration

ConvertInstantToLocalDate3.java

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

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

public class ConvertInstantToLocalDate3 {

	public static void main(String[] args) {

		// get instantaneous moment at UTC/GMT
		Instant instant = Instant.now();
		System.out.println("Current Instant at UTC/GMT is :- \n" + instant);


		// 1. convert Instant to OffsetDateTime
		OffsetDateTime offsetDateTime = OffsetDateTime
				.ofInstant(instant, ZoneOffset.UTC);
		System.out.println("\nOffsetDateTime is :- \n" + offsetDateTime);


		// 2. Instant -> OffsetDateTime -> LocalDate
		LocalDate localDate = offsetDateTime.toLocalDate();
		System.out.print("\nLocalDate is :- \n" + localDate);
	}
}

Output:

Current Instant at UTC/GMT is :- 
2022-08-19T15:12:54.191930400Z

OffsetDateTime is :- 
2022-08-19T15:12:54.191930400Z

LocalDate is :- 
2022-08-19

2. Convert LocalDate to an Instant :

Again, 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

  1. Convert LocalDate to an Instant via LocalDateTime
  2. Convert LocalDate to an Instant via ZonedDateTime
  3. Convert LocalDate to an Instant via OffsetDateTime

2.1 Convert LocalDate to an Instant via LocalDateTime :

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

ConvertLocalDateToInstant1.java

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

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

public class ConvertLocalDateToInstant1 {

	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 via LocalDateTime
		LocalDateTime localDateTime = localDate.atStartOfDay();
		Instant instant = localDateTime.toInstant(zoneOffset);
		System.out.print("\nConversion of LocalDate to an Instant via LocalDateTime is :- \n"
				+ instant);
	}
}

Output:

Current System Date is :- 
2022-08-19

UTC ZoneOffset is :- 
Z

Conversion of LocalDate to an Instant via LocalDateTime is :- 
2022-08-19T00:00:00Z

2.2 Convert LocalDate to an Instant via ZonedDateTime :

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

ConvertLocalDateToInstant2.java

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

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

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 system default Zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nSystem default Zone is :- \n" + zoneId);


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

Output:

Current System Date is :- 
2022-08-19

System default Zone is :- 
Asia/Calcutta

Conversion of LocalDate to an Instant via ZonedDateTime is :- 
2022-08-18T18:30:00Z

2.3 Convert LocalDate to an Instant via OffsetDateTime :

  • First step is to convert LocalDate to OffsetDateTime using atTime() method of LocalDate passing OffsetTime as argument
  • After converting to an OffsetDateTime, use/invoke toInstant() method of OffsetDateTime to convert into an Instant as shown in the below illustration

ConvertLocalDateToInstant3.java

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

import java.time.Instant;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;

public class ConvertLocalDateToInstant3 {

	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 current OffsetTime
		OffsetTime offsetTime = OffsetTime.now();
		System.out.println("\nCurrent OffsetTime is :- \n" + offsetTime);


		// 3. convert LocalDate to Instant via OffsetDateTime
		OffsetDateTime offsetDateTime = localDate.atTime(offsetTime);
		Instant instant = offsetDateTime.toInstant();
		System.out.print("\nConversion of LocalDate to an Instant via OffsetDateTime is :- \n"
				+ instant);
	}
}

Output:

Current System Date is :- 
2022-08-19

Current OffsetTime is :- 
20:43:37.048923800+05:30

Conversion of LocalDate to an Instant via OffsetDateTime is :- 
2022-08-19T15:13:37.048923800Z

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Instant to LocalTime ?
Java 8 – How to parse Instant in String form ?