Java 8 – How to convert Instant to LocalTime ?

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

  1. Convert Instant to LocalTime via LocalDateTime
  2. Convert Instant to LocalTime via ZonedDateTime
  3. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Instant to LocalDateTime ?
Java 8 – How to convert Instant to LocalDate ?