Finding time difference between two java.sql.Timestamps

Here is a simple method that can compare the difference in seconds, minutes, hours and days.

public static long compareTwoTimeStamps(java.sql.Timestamp currentTime, java.sql.Timestamp oldTime)
{
  long milliseconds1 = oldTime.getTime();
  long milliseconds2 = currentTime.getTime();

  long diff = milliseconds2 - milliseconds1;
  long diffSeconds = diff / 1000;
  long diffMinutes = diff / (60 * 1000);
  long diffHours = diff / (60 * 60 * 1000);
  long diffDays = diff / (24 * 60 * 60 * 1000);

  return diffMinutes;
}

Leave a Reply

Your email address will not be published. Required fields are marked *