In this article, we will learn different ways to convert an Instant to LocalTime using different methods provided in Java 1.8 version
For LocalTime to an Instant conversion, read Java 8 – How to convert LocalTime to an Instant ?
Convert Instant to LocalTime :
Direct conversion from Instant to LocalTime isn’t possible and hence convert Instant to either LocalDateTime/ZonedDateTime/OffsetDateTime and then use toLocalTime() method of respective classes to convert to LocalTime
- Convert Instant to LocalTime via LocalDateTime
- Convert Instant to LocalTime via ZonedDateTime
- Convert Instant to LocalTime via OffsetDateTime
1. Convert Instant to LocalTime via LocalDateTime :
- First step is to convert Instant to LocalDateTime using LocalDateTime.ofInstant() method passing instant & ZoneOffset
- After converting to LocalDateTime, use/invoke toLocalTime() method of LocalDateTime to convert into LocalTime as shown in the below illustration
ConvertInstantToLocalTime1.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
public class ConvertInstantToLocalTime1 {
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 -> LocalTime
LocalTime localTime = localDateTime.toLocalTime();
System.out.print("\nLocalTime is :- \n" + localTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:06:33.809926700Z
LocalDateTime is :-
2022-08-19T16:06:33.809926700
LocalTime is :-
16:06:33.809926700
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 toLocalTime() method of ZonedDateTime to convert into LocalTime as shown in the below illustration
ConvertInstantToLocalTime2.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
public class ConvertInstantToLocalTime2 {
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 -> LocalTime
LocalTime localTime = zonedDateTime.toLocalTime();
System.out.print("\nLocalTime is :- \n" + localTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:06:53.739015900Z
ZonedDateTime is :-
2022-08-19T16:06:53.739015900Z
LocalTime is :-
16:06:53.739015900
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 toLocalTime() method of OffsetDateTime to convert into LocalTime as shown in the below illustration
ConvertInstantToLocalTime3.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class ConvertInstantToLocalTime3 {
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 -> LocalTime
LocalTime localTime = offsetDateTime.toLocalTime();
System.out.print("\nLocalTime is :- \n" + localTime);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T16:07:13.319262100Z
OffsetDateTime is :-
2022-08-19T16:07:13.319262100Z
LocalTime is :-
16:07:13.319262100
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 !!