Compute and display elapsed time of an operation in Java

Description

The following code shows how to compute and display elapsed time of an operation.

Example


/*from ww w.  j  a  va  2  s  .c  o m*/
public class Main {
  public static void main(String[] args) throws Exception{
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < 10; i++) {
        Thread.sleep(60);
    }
    long endTime = System.currentTimeMillis();
    float seconds = (endTime - startTime) / 1000F;
    System.out.println(Float.toString(seconds) + " seconds.");
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone