Java 8 Clock millis() Method


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);

Updated on: 30-Jul-2019

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements