Open In App

LocalTime now(clock) method in Java with Examples

The now(Clock clock) method of the LocalTime class in Java is used to get the current time from the specified clock.

Syntax:



public static LocalTime now(
    Clock clock)

Parameters: This method accepts clock as parameter which represents the clock to use.

Return value: This method returns the current time.



Below programs illustrate the now(Clock clock) method of LocalTime in Java:

Program 1:




// Java program to demonstrate
// LocalTime.now(Clock clock) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // apply now(Clock clock) method
        // of LocalTime class
        LocalTime time = LocalTime.now(
            Clock.systemUTC());
  
        // print time
        System.out.println("Time: "
                           + time);
    }
}

Output:
Time: 21:04:28.811

Program 2:




// Java program to demonstrate
// LocalTime.now(Clock clock) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // apply now(Clock clock) method
        // of LocalTime class
        LocalTime time = LocalTime.now(
            Clock.systemUTC());
  
        // print time
        System.out.println("Time: "
                           + time);
    }
}

Output:
Time: 21:05:21.192

Above two examples are taken to show the variance in result for same program according to time progression.

References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#now(java.time.Clock)


Article Tags :