In Java, you can use the following ways to measure elapsed time in Java.

1. System.nanoTime()

This is the recommended solution to measure elapsed time in Java.

ExecutionTime1.java

package com.favtuts.time;

import java.util.concurrent.TimeUnit;

public class ElapsedTimeExecutionTime {

    public static void main(String[] args) throws InterruptedException {
        measuredElapsedTimeWithSystemNanoTime();
    }

    static void measuredElapsedTimeWithSystemNanoTime() throws InterruptedException {
        // start
        long lStartTime = System.nanoTime();

        // task
        calculation();

        // end
        long lEndTime = System.nanoTime();

        // time elapsed
        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output / 1000000);
    }

    private static void calculation() throws InterruptedException {

        // Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.

Elapsed time in milliseconds: 2000

2. System.currentTimeMillis()

ExecutionTime2.java

package com.favtuts.time;

import java.util.concurrent.TimeUnit;

public class ElapsedTimeExecutionTime {

    public static void main(String[] args) throws InterruptedException {
        measuredElapsedTimeWithSystemCurrentTimeMillis();
    }

    static void measuredElapsedTimeWithSystemCurrentTimeMillis() throws InterruptedException {
        long lStartTime = System.currentTimeMillis();

        calculation();

        long lEndTime = System.currentTimeMillis();

        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output);
    }

    private static void calculation() throws InterruptedException {

        // Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.

Elapsed time in milliseconds: 2000

3. Instant.now().toEpochMilli()

In Java 8, you can try the new java.time.Instant

ExecutionTime3.java

package com.favtuts.time;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class ElapsedTimeExecutionTime {

    public static void main(String[] args) throws InterruptedException {
        measuredElapsedTimeWithInstantNowToEpochMilli();
    }

    static void measuredElapsedTimeWithInstantNowToEpochMilli() throws InterruptedException {
        long lStartTime = Instant.now().toEpochMilli();

        calculation();

        long lEndTime = Instant.now().toEpochMilli();

        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output);
    }

    private static void calculation() throws InterruptedException {

        // Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.

Elapsed time in milliseconds: 2000

4. Date().getTime()

ExecutionTime4.java

package com.favtuts.time;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ElapsedTimeExecutionTime {

    public static void main(String[] args) throws InterruptedException {
        measuredElapsedTimeWithDateGetTime();
    }
    
    static void measuredElapsedTimeWithDateGetTime() throws InterruptedException {
        long lStartTime = new Date().getTime();

        calculation();

        long lEndTime = new Date().getTime();

        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output);
    }

    private static void calculation() throws InterruptedException {

        // Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output

Elapsed time in milliseconds: 2001

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-basic/time

References

  1. Stackoverflow – Is System.nanoTime() completely useless?
  2. Stackoverflow – System.currentTimeMillis vs System.nanoTime
  3. System#nanoTime JavaDoc
  4. Instant#toEpochMilli JavaDoc

Leave a Reply

Your email address will not be published. Required fields are marked *