In LocalTime class, there are three types of now() method depending upon the parameters passed to it.
now()
now() method of a LocalTime class used to obtain the current time from the system clock in the default time-zone.This method will return LocalTime based on system clock with default time-zone to obtain the current time.
Syntax:
public static LocalTime now()
Parameters: This method accepts no parameter.
Return value: This method returns the current time using the system clock.
Below programs illustrate the now() method:
Program 1:
// Java program to demonstrate // LocalTime.now() method import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create an LocalTime object
LocalTime lt
= LocalTime.now();
// print result
System.out.println( "LocalTime : "
+ lt);
}
} |
LocalTime : 05:47:38.946
now(Clock clock)
now(Clock clock) method of a LocalTime class used to return the current time based on the specified clock passed as parameter.
Syntax:
public static LocalTime now(Clock clock)
Parameters: This method accepts clock as parameter which is the clock to use.
Return value: This method returns the current time.
Below programs illustrate the now() method:
Program 1:
// Java program to demonstrate // LocalTime.now() method import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a clock
Clock cl = Clock.systemUTC();
// create an LocalTime object using now(Clock)
LocalTime lt
= LocalTime.now(cl);
// print result
System.out.println( "LocalTime : "
+ lt);
}
} |
LocalTime : 05:47:44.184
now(ZoneId zone)
now(ZoneId zone) method of a LocalTime class used to return the current time from system clock in the specified time-zone passed as parameter.Specifying the time-zone avoids dependence on the default time-zone.
Syntax:
public static LocalTime now(ZoneId zone)
Parameters: This method accepts zone as parameter which is the zone to use.
Return value: This method returns the current time.
Below programs illustrate the now() method:
Program 1:
// Java program to demonstrate // LocalTime.now() method import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a clock
ZoneId zid = ZoneId.of( "Europe/Paris" );
// create an LocalTime object using now(zoneId)
LocalTime lt
= LocalTime.now(zid);
// print result
System.out.println( "LocalTime : "
+ lt);
}
} |
LocalTime : 06:47:45.982
References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#now()
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#now(java.time.Clock)
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#now(java.time.ZoneId)