In this article, we will learn how to convert LocalTime to an Instant by adding/combining date & zone informations using atDate(), atZone() & toInstant() methods provided in Java 1.8 version
For Instant to LocalTime conversion, read Java 8 – How to convert Instant to LocalTime ?
Convert LocalTime to an Instant :
Direct conversion between LocalTime and Instant isn’t possible hence convert LocalTime to LocalDateTime using atDate() method and then convert LocalDateTime to ZonedDateTime using atZone() method and then finally invoke toInstant() method to convert ZonedDateTime to an Instant
- First, convert LocalTime to LocalDateTime using atDate() method –
- atDate(LocalDate) – This method combines invoking LocalTime with a provided LocalDate to create a
LocalDateTime
- atDate(LocalDate) – This method combines invoking LocalTime with a provided LocalDate to create a
- Second, convert LocalDateTime to an OffsetDateTime using atOffset() method –
- atOffset(ZoneOffset) – This method combines invoking date-time with an offset to create an OffsetDateTime
- Finally, convert ZonedDateTime to an Instant using toInstant() method –
- toInstant() – This method converts invoking date-time to an Instant
ConvertLocalTimeToInstant.java
package in.bench.resources.java8.localtime.examples;
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 :-
20:06:17.007319800
Current system date is :-
2022-08-23
Default time-zone is :-
Asia/Calcutta
Current Instant at UTC/GMT is :-
2022-08-23T14:36:17.007319800Z
Related Articles:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- 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/LocalTime.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/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!