令人讨厌的是,withTimeAtStartOfDay 的答案是错误的,但只是偶尔。你要:
Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()
事实证明,“午夜/一天的开始”有时意味着凌晨 1 点(在某些地方会以这种方式进行夏令时),而 Days.daysBetween 无法正确处理。
// 5am on the 20th to 1pm on the 21st, October 2013, Brazil
DateTimeZone 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 0
System.out.println(daysBetween(start.toLocalDate(),
end.toLocalDate()).getDays());
// prints 1
通过一个LocalDate
回避整个问题。