Measuring elapsed time in Java



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( ) + "
");          Thread.sleep(5*60*10);          System.out.println(new Date( ) + "
");          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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements