In this article, we will learn different ways to convert an Instant to LocalDateTime using different methods provided in Java 1.8 version
For LocalDateTime to an Instant conversion, read Java 8 – How to convert LocalDateTime to an Instant ?
1. Convert Instant to LocalDateTime :
Direct conversion from Instant to LocalDateTime is possible using LocalDateTime.ofInstant() method of LocalDateTime class or else both ZonedDateTime/OffsetDateTime classes has toLocalDateTime() method
- Convert Instant to LocalDateTime
- Convert Instant to LocalDateTime via ZonedDateTime
- Convert Instant to LocalDateTime via OffsetDateTime
1.1 Convert Instant to LocalDateTime :
- Convert Instant to LocalDateTime using LocalDateTime.ofInstant() method passing instant & ZoneOffset as input–argument
ConvertInstantToLocalDateTime1.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class ConvertInstantToLocalDateTime1 {
public static void main(String[] args) {
// 1. get instantaneous moment at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n" + instant);
// 2. convert Instant to LocalDateTime
LocalDateTime localDateTime = LocalDateTime
.ofInstant(instant, ZoneOffset.UTC);
System.out.print("\nLocalDateTime is :- \n" + localDateTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:25:10.351805600Z
LocalDateTime is :-
2022-08-19T16:25:10.351805600
1.2 Convert Instant to LocalDateTime via ZonedDateTime :
- First step is to convert Instant to ZonedDateTime using ZonedDateTime.ofInstant() method passing instant & ZoneOffset as input–arguments
- After converting to ZonedDateTime, use/invoke toLocalDateTime() method of ZonedDateTime to convert into LocalDateTime as shown in the below illustration
ConvertInstantToLocalDateTime2.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ConvertInstantToLocalDateTime2 {
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 -> LocalDateTime
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.print("\nLocalDateTime is :- \n" + localDateTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:25:45.825023200Z
ZonedDateTime is :-
2022-08-19T16:25:45.825023200Z
LocalDateTime is :-
2022-08-19T16:25:45.825023200
1.3 Convert Instant to LocalDateTime via OffsetDateTime :
- First step is to convert Instant to an OffsetDateTime using OffsetDateTime.ofInstant() method passing instant & ZoneOffset as input–arguments
- After converting to an OffsetDateTime, use/invoke toLocalDateTime() method of OffsetDateTime to convert into LocalDateTime as shown in the below illustration
ConvertInstantToLocalDateTime3.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.OffsetDateTime;
public class ConvertInstantToLocalDateTime3 {
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 -> LocalDateTime
LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
System.out.print("\nLocalDateTime is :- \n" + localDateTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:26:17.329992100Z
OffsetDateTime is :-
2022-08-19T16:26:17.329992100Z
LocalDateTime is :-
2022-08-19T16:26:17.329992100
2. Convert LocalDateTime to an Instant :
- Convert LocalDateTime to an Instant using toInstant() method of LocalDateTime class passing ZoneOffset as input–argument
- Read Java 8 – How to convert LocalDateTime to an Instant ? for more details and examples
ConvertLocalDateTimeToInstant.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class ConvertLocalDateTimeToInstant {
public static void main(String[] args) {
// 1. get current System Date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n" + localDateTime);
// 2. get system default zone offset
ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
System.out.println("\nZone Offset is :- \n" + zoneOffset);
// 3. convert LocalDateTime to an Instant using toInstant(zoneOffset)
Instant instant = localDateTime.toInstant(zoneOffset);
System.out.print("\nConversion of LocalDateTime to Instant is :- \n"
+ instant);
}
}
Output:
Current System Date/time is :-
2022-08-19T21:52:09.614339800
Zone Offset is :-
+05:30
Conversion of LocalDateTime to Instant is :-
2022-08-19T16:22:09.614339800Z
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/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
Happy Coding !!
Happy Learning !!