O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Calculating Elapsed Time

long start = System.currentTimeMillis();
											// do some other stuff...
											long end = System.currentTimeMillis();
											long elapsedTime = end – start;

By calculating elapsed time, we can determine how long it takes to do something or how long a process takes to complete. To do this, we use the System.currentTimeMillis() method to obtain the current time in milliseconds. We use this method at the start and end of the task we want to get the elapsed time for, and then take the difference in times. The actual value that is returned by the System.currentTimeMillis() method is the time since Janauary 1, 00:00:00, 1970 in milliseconds.

JDK 1.5 adds a nanoTime() method to the System class, which allows you to get even more precise timing, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required