Well, I'll try yet another code sample:
/**
* Calculates the number of FULL days between to dates
* @param startDate must be before endDate
* @param endDate must be after startDate
* @return number of day between startDate and endDate
*/
public static int daysBetween(Calendar startDate, Calendar endDate) {
long start = startDate.getTimeInMillis();
long end = endDate.getTimeInMillis();
// It's only approximation due to several bugs (@see java.util.Date) and different precision in Calendar chosen
// by user (ex. day is time-quantum).
int presumedDays = (int) TimeUnit.MILLISECONDS.toDays(end - start);
startDate.add(Calendar.DAY_OF_MONTH, presumedDays);
// if we still didn't reach endDate try it with the step of one day
if (startDate.before(endDate)) {
startDate.add(Calendar.DAY_OF_MONTH, 1);
++presumedDays;
}
// if we crossed endDate then we must go back, because the boundary day haven't completed yet
if (startDate.after(endDate)) {
--presumedDays;
}
return presumedDays;
}