LocalTime ofInstant() method in Java with Examples
The ofInstant() method of a LocalTime class is used to obtain an instance of LocalTime from an Instant and zone ID passed as parameters.In this method First, the offset from UTC/Greenwich is obtained using the zone ID and instant. Then, the local time has calculated from the instant and offset.
Syntax:
public static LocalTime
ofInstant(Instant instant, ZoneId zone)
Parameters: This method accepts two parameters:
- instant: It is the instant from which the LocalTime object is to be created. It should not be null.
- zone: It is the zone of the specified time. It should not be null.
Return value: This method returns the created LocalTime object created from the passed instant.
Below programs illustrate the ofInstant() method:
Program 1:
// Java program to demonstrate // LocalTime.ofInstant() method import java.time.*; public class GFG { public static void main(String[] args) { // create an Instant object Instant instant = Instant.parse("2018-12-17T19:59:44.770Z"); // print Instant System.out.println("Instant: " + instant); // create ZoneId ZoneId zoneid = ZoneId.systemDefault(); // print ZoneId System.out.println("ZoneId: " + zoneid); // apply ofInstant() LocalTime value = LocalTime.ofInstant(instant, zoneid); // print result System.out.println("Generated LocalTime: " + value); } } |
Ouput:
Instant: 2018-12-17T19:59:44.770Z ZoneId: Etc/UTC Generated LocalTime: 19:59:44.770
Program 2:
// Java program to demonstrate // LocalTime.ofInstant() method import java.time.*; public class GFG { public static void main(String[] args) { // create an Instant object Instant instant = Instant.parse("2016-11-11T09:19:22Z"); // print Instant System.out.println("Instant: " + instant); // apply ofInstant() LocalTime value = LocalTime.ofInstant(instant, ZoneId.of("Asia/Dhaka")); // print result System.out.println("Generated LocalTime: " + value); } } |
Output:
Instant: 2016-11-11T09:19:22Z Generated LocalTime: 15:19:22
Recommended Posts:
- ZonedDateTime ofInstant() method in Java with Examples
- OffsetTime ofInstant() method in Java with examples
- LocalTime with() Method in Java with Examples
- LocalTime get() method in Java with Examples
- LocalTime plus() method in Java with Examples
- LocalTime until() Method in Java with Examples
- LocalTime from() method in Java with Examples
- LocalTime hashCode() method in Java with Examples
- LocalTime parse() method in Java with Examples
- LocalTime truncatedTo() method in Java with Examples
- LocalTime format() method in Java with Examples
- LocalTime equals() method in Java with Examples
- LocalTime compareTo() method in Java with Examples
- LocalTime adjustInto() method in Java with Examples
- LocalTime atDate() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



