Instant from() method in Java with Examples
The from() method of Instant class helps to get instance of instant from TemporalAccessor object passed as parameter. A TemporalAccessor represents an arbitrary set of date and time information and this method helps to get an instant based on the specified TemporalAccessor object. The conversion of TempralAccessor object to instant extracts the INSTANT_SECONDS and NANO_OF_SECOND fields.
Syntax:
public static Instant
from(TemporalAccessor temporal)
Parameters: This method accepts only one parameter temporal which represents the temporal object. It should not be null.
Returns: This method returns Instant object from the TemporalAccessor Object. It should not be null.
Exception: This method throws DateTimeException if method is unable to convert temporal object to an Instant.
Below programs illustrate the from() method:
Program 1:
// Java program to demonstrate // Instant.from() method import java.time.*; public class GFG { public static void main(String[] args) { // create a ZonedDateTime object ZonedDateTime zonedDateTime = ZonedDateTime.now(); // print Value System.out.println("ZonedDateTime: " + zonedDateTime); // create a Instant object using // from() method Instant result = Instant.from(zonedDateTime); // print result System.out.println("Instant: " + result); } } |
ZonedDateTime: 2018-11-27T04:58:47.691Z[Etc/UTC] Instant: 2018-11-27T04:58:47.691Z
Program 2:
// Java program to demonstrate // Instant.from() method import java.time.*; public class GFG { public static void main(String[] args) { // create a OffsetDateTime object OffsetDateTime offset = OffsetDateTime.now(); // print Value System.out.println("OffsetDateTime: " + offset); // apply from() method Instant result = Instant.from(offset); // print result System.out.println("Instant: " + result); } } |
OffsetDateTime: 2018-11-27T04:58:50.588Z Instant: 2018-11-27T04:58:50.588Z
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#from(java.time.temporal.TemporalAccessor)
Recommended Posts:
- Instant plus() method in Java with Examples
- Instant get() method in Java with Examples
- Instant until() Method in Java with Examples
- Instant now() Method in Java with Examples
- Instant with() Method in Java with Examples
- Java 8 Clock instant() method with Examples
- Instant equals() method in Java with Examples
- Instant truncatedTo() method in Java with Examples
- Instant compareTo() method in Java with Examples
- Instant minusMillis() method in Java with Examples
- Instant isBefore() method in Java with Examples
- Instant isSupported() method in Java with Examples
- Instant atZone() method in Java with Examples
- Instant toString() method in Java with Examples
- Instant range() 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.

