令人讨厌的是,withTimeAtStartOfDay答案是错误的,但只是偶尔出现。你要:
Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()
事实证明,“midnight/start 的开始时间”有时是指凌晨1点(某些地方以这种方式实现夏令时),而Days.daysBetween无法正确处理。
// 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通过走出去
LocalDate回避了整个问题。



