查看 Joda Time源代码。Days的daysBetween
方法定义为:
public static Days daysBetween(ReadableInstant start, ReadableInstant end) {
int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.days());
return Days.days(amount);
}
BaseSingleFieldPeriod
,像 Days 一样,在 Joda Time 1.2 中不可用,但是,查看它在 1.2 中可用的源类开始出现:
protected static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) {
if (start == null || end == null) {
throw new IllegalArgumentException("ReadableInstant objects must not be null");
}
Chronology chrono = DateTimeUtils.getInstantChronology(start);
int amount = field.getField(chrono).getDifference(end.getMillis(), start.getMillis());
return amount;
}
所有这些类和方法都在 Joda Time 1.2 中可用,因此计算两个实例之间的天数类似于:
public static int daysBetween(ReadableInstant oldDate, ReadableInstant today) {
Chronology chrono = DateTimeUtils.getInstantChronology(oldDate);
int amount = DurationFieldType.days().getField(chrono).getDifference(today.getMillis(), oldDate.getMillis());
return amount;
}