3

I'm retrieving two timestamps from a database (check_in and check_out). I need to compare the two to find out how many days have passed between the two timestamps. How can this be done in Java?

Daal
  • 79
  • 2
  • 2
  • 7
  • Since Java 8, calculating the difference is more intuitive if we use LocalDate, LocalDateTime to represent the two dates (with or without time) in combination with Period and Duration: Timestamp date1 = (Timestamp) mTab.getValue("DateFrom"); LocalDate dateFrom = date1.toLocalDateTime().toLocalDate(); Timestamp date2 = (Timestamp) mTab.getValue("DateTo"); LocalDate dateTo = date2.toLocalDateTime().toLocalDate(); Period period = Period.between(dateFrom, dateTo); int diff = period.getDays(); – Mohamed Dernoun Dec 07 '18 at 21:07

4 Answers4

7

Just subtract check_out and check_in, and convert from whatever units you're in to days. Something like

//pseudo-code
diff_in_millis = Absolute Value Of ( check_out - check_in ) ;
diff_in_days = diff_in_millis / (1000  * 60 * 60 * 24);
matiash
  • 54,791
  • 16
  • 125
  • 154
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • This had given me some headache if the two timestamps come from different hosts. A few millis difference on a day border (12:00) can lead to different values. – yottamoto Feb 05 '13 at 12:35
2

Following code snippet also will give days difference

private int getDaysBetween (Timestamp start, Timestamp end)   {

      boolean negative = false;
      if (end.before(start))  {
          negative = true;
          Timestamp temp = start;
          start = end;
          end = temp;
      }

      GregorianCalendar cal = new GregorianCalendar();
      cal.setTime(start);
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
      cal.set(Calendar.MILLISECOND, 0);

      GregorianCalendar calEnd = new GregorianCalendar();
      calEnd.setTime(end);
      calEnd.set(Calendar.HOUR_OF_DAY, 0);
      calEnd.set(Calendar.MINUTE, 0);
      calEnd.set(Calendar.SECOND, 0);
      calEnd.set(Calendar.MILLISECOND, 0);


      if (cal.get(Calendar.YEAR) == calEnd.get(Calendar.YEAR))   {
          if (negative)
               return (calEnd.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR)) * -1;
          return calEnd.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR);
      }

      int days = 0;
      while (calEnd.after(cal))    {
          cal.add (Calendar.DAY_OF_YEAR, 1);
          days++;
      }
      if (negative)
          return days * -1;
      return days;
  }
Giri
  • 507
  • 6
  • 23
0

I sort my Set myClassSet by date do as follows:

private Set<MyClass> orderLocation(Set<MyClass> myClassSet) {

    List<MyClass> elementList = new ArrayList<MyClass>();
    elementList.addAll(myClassSet);

    // Order by date
    Collections.sort(elementList, this.comparatorElements);
    Set<MyClass> l = new TreeSet<MyClass>(comparatorElements);
    l.addAll(elementList);

    return l;
}

private final Comparator<MyClass> comparatorElements = new Comparator<MyClass>() {
    @Override
    public int compare(MyClass element1, MyClass element2) {

        long l1 = element1.getDate().getTime();
        long l2 = element2.getDate().getTime();
        if (l2 > l1)
        return 1;
        else if (l1 > l2)
        return -1;
        else
        return 0;
    }
 };
borchvm
  • 3,533
  • 16
  • 44
  • 45
0
int frequency=0;
 Calendar cal1 = new GregorianCalendar();
 Calendar cal2 = new GregorianCalendar();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
 Date date = sdf.parse("2014-11-01 04:04:51.451975");
 cal1.setTime(date);
 date =  sdf.parse("2015-11-30 04:04:51.451975");
 cal2.setTime(date);

Date d1,d2;
d1=cal1.getTime();
d2=cal2.getTime();
frequency =(int)(TimeUnit.DAYS.convert(d2.getTime() - d1.getTime(), TimeUnit.MILLISECONDS));

    System.out.println ("Days: " + frequency);
bhanu
  • 1