How to calculate elapsed / execute time in Java

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.mkyong.time;

import java.util.concurrent.TimeUnit;

public class ExecutionTime1 {

    public static void main(String[] args) 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.


2004

2. System.currentTimeMillis()

ExecutionTime2.java

package com.mkyong.time;

import java.util.concurrent.TimeUnit;

public class ExecutionTime2 {

    public static void main(String[] args) 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.


2006

3. Instant.now().toEpochMilli()

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

ExecutionTime3.java

package com.mkyong.time;

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

public class ExecutionTime3 {

    public static void main(String[] args) 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.


2006

4. Date().getTime()

ExecutionTime4.java

package com.mkyong.time;

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

public class ExecutionTime4 {

    public static void main(String[] args) 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 may vary.


2007

References

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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
programer
Guest
programer

hi

Vinayak Srivastava
Guest
Vinayak Srivastava

Android docs for System.currentTimeMillis() shows as
“This method shouldn’t be used for measuring timeouts or other elapsed time
measurements, as changing the system time can affect the results. Use nanoTime() for
that.”

Farooq
Guest
Farooq

output is different ever time execution time is calculates. as you also mentioned that output may vary. what is the reason for that .

programer
Guest
programer

hi, I’m trying to collect tweets in particular time how can I make it, thank you

Shaikh Nikhat
Guest
Shaikh Nikhat

I am getting java.lang.OutOfMemoryError : java heap space

Jeryl Cook
Guest
Jeryl Cook

kinda of hard to do that with the above example…copy and paste the code?

trackback
Java – Time elapsed in days, hours, minutes, seconds

[…] How To Calculate Elapsed / Execute Time In Java […]

Ade Malsasa Akbar
Guest
Ade Malsasa Akbar

But how if i wanna count total minutes from two different time? Times are start hour:minute and end hour:minute? Please give an answer copy to my blog. Thank you…

Anonymosu
Guest
Anonymosu

You can also use StopWatch class from Spring to calculate time difference as shown in this article.

mydhinda
Guest
mydhinda

Expected java would have something in built.
But this is a good approach too !
Thanks for sharing.

Kouassi
Guest
Kouassi

What is the time equivalent in Swiss if CET time is reading at 2:30pm ?

How do we calculate other time difference from java?