如何处理 LocalDate Period.between 结果?
How to handle LocalDate Period.between results?
我使用 LocalDate
(ThreeTenABP) 来计算给定日期之间的时间间隔。当我使用 01/01/2018 作为第一个日期和 12/31/2018 作为第二个日期时,Period.between
给出以下结果:
00 years
11 months
30 days
恕我直言,这是错误的,因为一整年有 12 个月。
我试着增加一天。然后我得到:
00 years
11 months
31 days
我需要一种可靠的方法来获得给定期间内完整月份的实际数量。
如果您查看 Period.between()
的 Javadoc,它会告诉您结束日期是 exclusive(意思是:不计算在内)。
The start date is included, but the end date is not. The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign.
我写了这段代码,它似乎按照我的预期运行...
LocalDate d1 = LocalDate.of(2018, 1, 1);
LocalDate d2 = LocalDate.of(2018, 12, 31);
Period p1 = Period.between(d1, d2);
System.out.println(p1.getYears() + " years, " + p1.getMonths() + " months, " + p1.getDays() + " days");
// Prints: 0 years, 11 months, 30 days
加上一天,即 2019-01-01,我一年:
LocalDate d3 = LocalDate.of(2019, 1, 1);
Period p2 = Period.between(d1, d3);
System.out.println(p2.getYears() + " years, " + p2.getMonths() + " months, " + p2.getDays() + " days");
// Prints: 1 years, 0 months, 0 days
编辑:
如果您只是将一天添加到计算的 Period
对象,您实际上只是添加一天,而不是像 between
方法那样重新计算周期。这是来自 Period
的代码,它执行 plusDays()
:
public Period plusDays(long daysToAdd) {
if (daysToAdd == 0) {
return this;
}
return create(years, months, Math.toIntExact(Math.addExact(days, daysToAdd)));
}
如果您按照 create
调用,它实际上只是将一天添加到天数计数器,它不会重新计算任何内容。要正确地将一天添加到一个时间段,请使用我上面的不同端点重新计算它。
靠谱的方法是:
LocalDate begin = LocalDate.of(2018, Month.JANUARY, 1);
LocalDate end = LocalDate.of(2018, Month.DECEMBER, 31);
Period inclusive = Period.between(begin, end.plusDays(1));
System.out.println(inclusive);
这会打印
P1Y
瞧瞧,一年的时间。在结束日期上加 1 天使结束日期包含在内(因为现在是结束日期之后的第二天)。
我使用 LocalDate
(ThreeTenABP) 来计算给定日期之间的时间间隔。当我使用 01/01/2018 作为第一个日期和 12/31/2018 作为第二个日期时,Period.between
给出以下结果:
00 years
11 months
30 days
恕我直言,这是错误的,因为一整年有 12 个月。 我试着增加一天。然后我得到:
00 years
11 months
31 days
我需要一种可靠的方法来获得给定期间内完整月份的实际数量。
如果您查看 Period.between()
的 Javadoc,它会告诉您结束日期是 exclusive(意思是:不计算在内)。
The start date is included, but the end date is not. The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign.
我写了这段代码,它似乎按照我的预期运行...
LocalDate d1 = LocalDate.of(2018, 1, 1);
LocalDate d2 = LocalDate.of(2018, 12, 31);
Period p1 = Period.between(d1, d2);
System.out.println(p1.getYears() + " years, " + p1.getMonths() + " months, " + p1.getDays() + " days");
// Prints: 0 years, 11 months, 30 days
加上一天,即 2019-01-01,我一年:
LocalDate d3 = LocalDate.of(2019, 1, 1);
Period p2 = Period.between(d1, d3);
System.out.println(p2.getYears() + " years, " + p2.getMonths() + " months, " + p2.getDays() + " days");
// Prints: 1 years, 0 months, 0 days
编辑:
如果您只是将一天添加到计算的 Period
对象,您实际上只是添加一天,而不是像 between
方法那样重新计算周期。这是来自 Period
的代码,它执行 plusDays()
:
public Period plusDays(long daysToAdd) {
if (daysToAdd == 0) {
return this;
}
return create(years, months, Math.toIntExact(Math.addExact(days, daysToAdd)));
}
如果您按照 create
调用,它实际上只是将一天添加到天数计数器,它不会重新计算任何内容。要正确地将一天添加到一个时间段,请使用我上面的不同端点重新计算它。
靠谱的方法是:
LocalDate begin = LocalDate.of(2018, Month.JANUARY, 1);
LocalDate end = LocalDate.of(2018, Month.DECEMBER, 31);
Period inclusive = Period.between(begin, end.plusDays(1));
System.out.println(inclusive);
这会打印
P1Y
瞧瞧,一年的时间。在结束日期上加 1 天使结束日期包含在内(因为现在是结束日期之后的第二天)。