Measuring elapsed time in Java

Java Programming Java8Object Oriented Programming

Sometimes, you may need to measure point in time in milliseconds. So let's re-write the above example once again −

Example

Live Demo

import java.util.*;
public class DiffDemo {

   public static void main(String args[]) {
      try {
         long start = System.currentTimeMillis( );
         System.out.println(new Date( ) + "\n");

         Thread.sleep(5*60*10);
         System.out.println(new Date( ) + "\n");

         long end = System.currentTimeMillis( );
         long diff = end - start;
         System.out.println("Difference is : " + diff);
      } catch (Exception e) {
         System.out.println("Got an exception!");
      }
   }
}

This will produce the following result −

Output

Sun May 03 18:16:51 GMT 2009
Sun May 03 18:16:57 GMT 2009
Difference is : 5993
raja
Published on 25-Jul-2018 12:04:44
Advertisements