Create a gist now

Instantly share code, notes, and snippets.

long startTime = System.nanoTime();
method();
long stopTime = System.nanoTime();
long millis = stopTime - startTime;
public String getDurationFormattedString(long startTime, long stopTime) {
final long nanosDifference = stopTime - startTime;
return String.format("%02d min, %02d sec %02d millis",
TimeUnit.NANOSECONDS.toMinutes(nanosDifference),
TimeUnit.NANOSECONDS.toSeconds(nanosDifference) -
TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(nanosDifference)),
TimeUnit.NANOSECONDS.toMillis(nanosDifference)
);
System.out.println(getDurationFormattedString(startTime, stopTime));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment