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 input–argument
- 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:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html
Happy Coding !!
Happy Learning !!