title: How to calculate the difference between two Java java.sql.Timestamps?
body: <p>Please include the nanos, otherwise it would be trivial:</p>
  
  <pre><code>long diff = Math.abs(t1.getTime () - t2.getTime ());
  </code></pre>
  
  <p>[EDIT] I want the most precise result, so no doubles; only integer/long arithmetic. Also, the result must be positive. Pseudo code:</p>
  
  <pre><code>Timestamp result = abs (t1 - t2);
  </code></pre>
  
  <p>Examples:</p>
  
  <pre><code>t1 = (time=1001, nanos=1000000), t2 = (time=999, nanos=999000000)
   -&gt; diff = (time=2, nanos=2000000)
  </code></pre>
  
  <p>Yes, milliseconds in java.sql.Timestamp are duplicated in the time and the nanos par, so 1001 milliseconds means 1 second (1000) and 1 milli which is in the <code>time</code> part and the <code>nanos</code> part because 1 millisecond = 1000000 nanoseconds). This is much more devious than it looks.</p>
  
  <p>I suggest not to post an answer without actually testing the code or having a working code sample ready :)</p>