Number of days between two dates in Joda-Time
Annoyingly, the withTimeAtStartOfDay answer is wrong, but only occasionally. You want:
Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()
It turns out that "midnight/start of day" sometimes means 1am (daylight savings happen this way in some places), which Days.daysBetween doesn't handle properly.
// 5am on the 20th to 1pm on the 21st, October 2013, BrazilDateTimeZone BRAZIL = DateTimeZone.forID("America/Sao_Paulo");DateTime start = new DateTime(2013, 10, 20, 5, 0, 0, BRAZIL);DateTime end = new DateTime(2013, 10, 21, 13, 0, 0, BRAZIL);System.out.println(daysBetween(start.withTimeAtStartOfDay(), end.withTimeAtStartOfDay()).getDays());// prints 0System.out.println(daysBetween(start.toLocalDate(), end.toLocalDate()).getDays());// prints 1
Going via a LocalDate
sidesteps the whole issue.
Days
Class
Using the Days
class with the withTimeAtStartOfDay
method should work:
Days.daysBetween(start.withTimeAtStartOfDay() , end.withTimeAtStartOfDay() ).getDays()