org.joda.time.Period 尊重闰年吗?

Does org.joda.time.Period respect leap years?

我正在做一些单元测试并且遇到过这个:

2015(非闰年)

LocalDate endDate = LocalDate.parse("01/03/2015", new DateTimeFormatterFactory("dd/MM/yyyy").createDateTimeFormatter());
LocalDate startDate = LocalDate.parse("25/02/2015", new DateTimeFormatterFactory("dd/MM/yyyy").createDateTimeFormatter());

org.joda.time.Period.fieldDifference(startDate, endDate).getDays(); // is -24

2016(闰年)

LocalDate endDate = LocalDate.parse("01/03/2016", new DateTimeFormatterFactory("dd/MM/yyyy").createDateTimeFormatter());
LocalDate startDate = LocalDate.parse("25/02/2016", new DateTimeFormatterFactory("dd/MM/yyyy").createDateTimeFormatter());

org.joda.time.Period.fieldDifference(startDate, endDate).getDays(); // is ALSO -24

我希望这些值至少是不同的。

有什么想法吗?

fieldDifference 的文档说

Calculation by field difference works by extracting the difference one field at a time and not wrapping into other fields. Thus 2005-06-09/2007-04-12 will yield P1Y-2M3D.

“01/03/2016”和“25/02/2016”之间天字段的差异是1 - 25 = -24。这并不是说那是两个日期之间的天数。 -24 作为任何一年中这两个日期之间的天数没有意义。

要计算两个日期之间的天数,您可以使用

org.joda.time.Days.daysBetween(startDate, endDate).getDays()

如果超过 2 月底,2015 年与 2016 年的数字确实会 return 不同。