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, down to nanoseconds. In reality, not all platforms support nanosecond resolution, so although the nanoTime() method may be available, you can't always count on getting nanosecond resolution. This is often useful for testing, profiling, and performance monitoring. |