Java 9 – How to convert Instant to LocalTime using ofInstant() method ?

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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert java.util.Date to LocalTime in different ways ?
Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?