Instant now() Method in Java with Examples
In Instant class, there are two types of now() method depending upon the parameters passed to it.
now()
now() method of a Instant class used to obtain the current instant from the system UTC clock.This method will return instant based on system UTC clock.
Syntax:
public static Instant now()
Parameters: This method accepts no parameter.
Return value: This method returns the current instant using the system clock
Below programs illustrate the now() method:
Program 1:
// Java program to demonstrate // Instant.now() method import java.time.*; public class GFG { public static void main(String[] args) { // create an Instant object Instant lt = Instant.now(); // print result System.out.println("Instant : " + lt); } } |
Instant : 2019-01-21T05:47:26.853Z
now(Clock clock)
now(Clock clock) method of a Instant class used to return the current instant based on the specified clock passed as parameter.
Syntax:
public static Instant now(Clock clock)
Parameters: This method accepts clock as parameter which is the clock to use.
Return value: This method returns the current instant.
Below programs illustrate the now() method:
Program 1:
// Java program to demonstrate // Instant.now() method import java.time.*; public class GFG { public static void main(String[] args) { // create a clock Clock cl = Clock.systemUTC(); // create an Instant object using now(Clock) Instant lt = Instant.now(cl); // print result System.out.println("Instant : " + lt); } } |
Instant : 2019-01-21T05:47:29.886Z
References:
https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#now()
https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#now(java.time.Clock)
Recommended Posts:
- Instant plus() method in Java with Examples
- Instant from() method in Java with Examples
- Instant get() method in Java with Examples
- Instant with() Method in Java with Examples
- Instant until() Method in Java with Examples
- Instant isAfter() method in Java with Examples
- Instant hashCode() method in Java with Examples
- Instant toEpochMilli() method in Java with Examples
- Instant toString() method in Java with Examples
- Instant ofEpochMilli() method in Java with Examples
- Instant plusSeconds() method in Java with Examples
- Instant parse() method in Java with Examples
- Instant minusNanos() method in Java with Examples
- Instant minusSeconds() method in Java with Examples
- Instant minusMillis() 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.



