title: Calculate elapsed time with System.currentTimeMillis() in java
body: <p>For some reason I can get the start time and the stop time in my program (which is normally a difference of 3 milliseconds) but when I print the result of the <code>getElapsedTime()</code> method it always comes back with 0. Above the listed code is the creation of a <code>StopWatch</code> object named <code>sortTime</code>. </p>
  
  <p>Perhaps there is a problem with rounding?
  Perhaps there is a problem with the <code>getElapsedTime()</code> method itself?</p>
  
  <pre><code>    System.out.println(sortTime.start()); // Start Timer
  
      java.util.Arrays.sort(numbers); // Sort the array
  
      System.out.println(sortTime.stop());// stop timer
  
      System.out.println("the elapsed time is: " + sortTime.getElapsedTime());
      }
  } //end of main class (used for testing)
  
  //Define Class
   class StopWatch {
   long startTime;
   long endTime;
  
   //Constructors
   public StopWatch () { 
   }
  
   //Methods
  long start() {
      long startTime = System.currentTimeMillis();
      return startTime; 
  }
  long stop() {
      long endTime = System.currentTimeMillis();
      return endTime; 
  }
  long getElapsedTime() {
          return (endTime - startTime);
      }
       }
  </code></pre>
  
  <p>For example, when I run the program the result is:</p>
  
  <p>1442877972466 <br />
  1442877972469 <br />
  the elapsed time is: 0</p>