Java 8 Clock millis() Method

Java 8ProgrammingObject Oriented Programming

The current instance of the clock in milliseconds can be obtained using the method millis() in the Clock Class in Java. This method requires no parameters and it returns the current instant of the clock in milliseconds. If the instance cannot be obtained for some reason, then the DateTimeException is thrown.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Clock c = Clock.systemDefaultZone();
      long ms = c.millis();
      System.out.println("The clock is: " + c);
      System.out.println("The instance in milliseconds is: " + ms);
   }
}

Output

The clock is: SystemClock[Etc/UTC]
The instance in milliseconds is: 1549529350787

Now let us understand the above program.

The method millis() is used to obtain the current instance of the clock in milliseconds and then this is displayed. A code snippet that demonstrates this is as follows −

Clock c = Clock.systemDefaultZone();
long ms = c.millis();
System.out.println("The clock is: " + c);
System.out.println("The instance in milliseconds is: " + ms);
raja
Published on 13-Mar-2019 15:38:20
Advertisements