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
why we use TimeUnit.SECONDS.sleep(2);
hi, I’m trying to collect tweets in particular time how can I make it, thank you
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 )
output is different ever time execution time is calculates. as you also mentioned that output may vary. what is the reason for that .
hi
I am getting java.lang.OutOfMemoryError : java heap space
kinda of hard to do that with the above example…copy and paste the code?
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.”
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…
You can also use StopWatch class from Spring to calculate time difference as shown in this article.
Expected java would have something in built.
But this is a good approach too !
Thanks for sharing.
What is the time equivalent in Swiss if CET time is reading at 2:30pm ?
How do we calculate other time difference from java?