LocalTime ofInstant() method in Java with Examples
Last Updated :
10 Jul, 2020
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:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-12-17T19:59:44.770Z" );
System.out.println( "Instant: " + instant);
ZoneId zoneid = ZoneId.systemDefault();
System.out.println( "ZoneId: " + zoneid);
LocalTime value
= LocalTime.ofInstant(instant, zoneid);
System.out.println( "Generated LocalTime: "
+ value);
}
}
|
Output:
Instant: 2018-12-17T19:59:44.770Z
ZoneId: Etc/UTC
Generated LocalTime: 19:59:44.770
Program 2:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2016-11-11T09:19:22Z" );
System.out.println( "Instant: " + instant);
LocalTime value
= LocalTime.ofInstant(instant,
ZoneId.of( "Asia/Dhaka" ));
System.out.println( "Generated LocalTime: "
+ value);
}
}
|
Output:
Instant: 2016-11-11T09:19:22Z
Generated LocalTime: 15:19:22
References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#ofInstant(java.time.Instant, java.time.ZoneId)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...