In this article, we will learn how to convert an Instant to LocalTime using newly introduced method in Java 1.9 version
1. Convert Instant to LocalTime in Java 1.9 version :
- Direct conversion between LocalTime and Instant isn’t possible till Java 1.8 version
- But in Java 1.9 version a new method LocalTime.ofInstant() introduced for direct conversion from Instant to LocalTime
- LocalTime.ofInstant(Instant, ZoneId) – gets an instance of LocalTime from an Instant and zone ID passed
ConvertInstantToLocalTime.java
package in.bench.resources.java9.conversion;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
public class ConvertInstantToLocalTime {
public static void main(String[] args) {
// 1. get current Instant
Instant instant = Instant.now();
System.out.println("Current Instant at GMT/UTC is :- \n"
+ instant);
// 2. get default time zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault time-zone is :- \n"
+ zoneId);
// 3. Second, Instant -> LocalTime
LocalTime localTime = LocalTime
.ofInstant(instant, zoneId);
System.out.print("\nLocalTime is :- \n"
+ localTime);
}
}
Output:
Current Instant at GMT/UTC is :-
2022-08-24T03:41:53.721001900Z
Default time-zone is :-
Asia/Calcutta
LocalTime is :-
09:11:53.721001900
2. Convert LocalTime to an Instant :
Direct conversion between LocalTime & Instant isn’t possible therefore convert LocalDate to LocalDateTime/ZonedDateTime/OffsetDateTime and then use/invoke atDate(), atZone() or toInstant() methods of respective classes to convert to an Instant
- First step is to convert LocalTime to LocalDateTime using atDate() method of LocalTime class passing LocalDate as input-argument
- After converting to LocalDateTime, use/invoke atZone() method of LocalDateTime passing ZoneId as argument to convert into ZonedDateTime
- After converting to ZonedDateTime, use/invoke toInstant() method of ZonedDateTime to convert into an Instant as shown in the below illustration
ConvertLocalTimeToInstant.java
package in.bench.resources.java9.conversion;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
public class ConvertLocalTimeToInstant {
public static void main(String[] args) {
// 1. get current system time
LocalTime localTime = LocalTime.now();
System.out.println("Current system time is :- \n"
+ localTime);
// 2. get current system date
LocalDate localDate = LocalDate.now();
System.out.println("\nCurrent system date is :- \n"
+ localDate);
// 3. get default time zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault time-zone is :- \n"
+ zoneId);
// 4. convert LocalTime -> LocalDateTime -> ZonedDateTime -> Instant
Instant instant = localTime // LocalTime
.atDate(localDate) // LocalDateTime
.atZone(zoneId) // ZonedDateTime
.toInstant(); // Instant
System.out.print("\nCurrent Instant at UTC/GMT is :- \n"
+ instant);
}
}
Output:
Current system time is :-
09:19:46.943940100
Current system date is :-
2022-08-24
Default time-zone is :-
Asia/Calcutta
Current Instant at UTC/GMT is :-
2022-08-24T03:49:46.943940100Z
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/9/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Instant.html
Happy Coding !!
Happy Learning !!