Current Time in Milliseconds and Nanoseconds

This section provides a tutorial example on how to obtain the current time in milliseconds and nanoseconds using currentTimeMillis() and nanoTime() methods.

If you want to measure how long some code takes to execute, you can use System.currentTimeMillis() and System.nanoTime() methods. They returns the current time in milliseconds and in nanoseconds.

Here is tutorial example program to check the performance of the Math.sqrt() method:

/* SystemCurrentTime.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class SystemCurrentTime {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      out.println("Performance for 10,000 operations:");
      out.println("   in milliseconds: "+milliseconds(10000));
      out.println("   in nanoseconds: "+nanoseconds(10000));

      out.println("Performance for 10,000,000 operations:");
      out.println("   in milliseconds: "+milliseconds(10000000));
      out.println("   in nanoseconds: "+nanoseconds(10000000));
   }
   public static long milliseconds(long max) {
      // Current time in milliseconds
      long time1 = System.currentTimeMillis();
      for (int i=0; i<max; i++){
        double input = 7.0;
        double output = Math.sqrt(input);
      }
      long time2 = System.currentTimeMillis();
      return time2 - time1;
   }
   public static long nanoseconds(long max) {
      // Current time in nanoseconds
      long time1 = System.nanoTime();
      for (int i=0; i<max; i++){
        double input = 7.0;
        double output = Math.sqrt(input);
      }
      long time2 = System.nanoTime();
      return time2 - time1;
   }
}

When executed with HotSpot 13 on my macOS computer, I got the following result:

herong> java SystemCurrentTime

Performance for 10,000 operations:
   in milliseconds: 0
   in nanoseconds: 295397
Performance for 10,000,000 operations:
   in milliseconds: 9
   in nanoseconds: 6561837

herong> java SystemCurrentTime
Performance for 10,000 operations:
   in milliseconds: 0
   in nanoseconds: 309228
Performance for 10,000,000 operations:
   in milliseconds: 9
   in nanoseconds: 6373549

Performance for 10,000 operations:
   in milliseconds: 0
   in nanoseconds: 285428
Performance for 10,000,000 operations:
   in milliseconds: 10
   in nanoseconds: 6332630

Note that nanoTime() method gives much better resolution, but its reporting time does not match well with the currentTimeMillis() method. For example, nanoTime() reported 6,332,630 nanoseconds, about 6 milliseconds; however currentTimeMillis() reported 10 milliseconds.

Here are some result I got from HotSpot 1.7 on my Windows computer:

herong> java SystemCurrentTime

Performance for 1000 operations:
   in milliseconds: 0
   in nanoseconds: 23423
Performance for 1000000 operations:
   in milliseconds: 8
   in nanoseconds: 7679683

herong> java SystemCurrentTime
Performance for 1000 operations:
   in milliseconds: 0
   in nanoseconds: 23011
Performance for 1000000 operations:
   in milliseconds: 7
   in nanoseconds: 7661191

herong> java SystemCurrentTime
Performance for 1000 operations:
   in milliseconds: 0
   in nanoseconds: 24655
Performance for 1000000 operations:
   in milliseconds: 7
   in nanoseconds: 7526821

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 java.lang.Runtime Class - The JVM Instance

java.lang.System Class - The Operating System

 What Is java.lang.System

 Standard Input, Output, and Error Streams

Current Time in Milliseconds and Nanoseconds

 Accessing System Environment Variables

 Getting and Adding System Properties

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 JVM Runtime Data Areas

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 Archived Tutorials

 References

 Full Version in PDF/EPUB