Instant now() Method in Java with Examples
Last Updated :
21 Jan, 2019
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:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant lt
= Instant.now();
System.out.println( "Instant : "
+ lt);
}
}
|
Output:
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:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Clock cl = Clock.systemUTC();
Instant lt
= Instant.now(cl);
System.out.println( "Instant : "
+ lt);
}
}
|
Output:
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...