Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ankit
5 years ago

why we use TimeUnit.SECONDS.sleep(2);

programer
7 years ago

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

Sissi
1 year ago

So that we verify that the function actually does calculate the time right (As seen we sleep the program for 2 sec and indeed the calculation shows 2006 ms ~ 2sec )

Farooq
6 years ago

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

programer
7 years ago

hi

Shaikh Nikhat
9 years ago

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

Jeryl Cook
9 years ago
Reply to  Shaikh Nikhat

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

Vinayak Srivastava
9 years ago

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.”

Ade Malsasa Akbar
11 years ago

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
11 years ago

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

mydhinda
11 years ago

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

Kouassi
11 years ago

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

How do we calculate other time difference from java?