title: How to get time (hh:mm:ss) from two different dates in java
body: <p>i am getting milleseconds from two different dates, trying to convert milleseconds to timestamp. i have written the following code.</p>
  
  <pre><code>    Date d1 = new Date();
      for(int i=0;i&lt;1000000;i++){
          System.out.println(i);
      }
  
      Date d2 = new Date();
      System.out.println(d1);
      System.out.println(d2);
  
      long seconds = d2.getTime()-d1.getTime();
      Date d3 = new Date(seconds);
  
      DateFormat df = new SimpleDateFormat("HH:mm:ss");
      System.out.println(seconds);
      System.out.println("Time is : "+df.format(d3));
  
      final Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis(seconds);
      System.out.println(df.format(cal.getTime()));
  </code></pre>
  
  <p>the for loop would take 2 or 3 seconds, the result may be 00:03:80, but my result always getting </p>
  
  <pre><code>Time is : 05:30:04
  05:30:04
  </code></pre>
  
  <p>but actual difference is 2 or 3 second, is this correct way or is there any other way.</p>