有没有办法让 Java8 持续一年来计算闰年?

Is there a way to have a Java8 duration of one year that accounts for leap years?

我需要一年中的天数,我想使用 Java8 的新时间 api。

但是,我不能 Duration.ofDays(365) 因为它不考虑闰年。 Duration.of(1, ChronoUnit.YEARS) 不会飞是因为 java.time.temporal.UnsupportedTemporalTypeException: Unit must not have an estimated duration

我查看了 Period,但它似乎对从几年到几天没有用。

我觉得我在这里遗漏了什么?如果这一年是闰年,我可以写一些东西来添加一天,但看起来我应该能够开箱即用。

根据 Getting Duration using the new dateTime API 中的回复,您应该使用

Period p = Period.ofYears(1);

了解 Duration(精确的纳秒数 < 1 天)和 Period(变量 > 1 天)之间的区别很重要。

例如,

Duration 不会考虑闰日、夏令时或闰秒,持续时间少于一天,最多几天。

所以你应该改用Period

因为不同的年份有不同的天数,如果你想找到一年中的天数,你需要指定你在说的是哪一年。

如果你想要特定年份的天数,你可以使用

Year.of(year).length()

如果你想要一年后的日期,你可以使用

LocalDate.now().plusYears(1)

LocalDate.now().plus(Period.ofYears(1))

如果需要两个日期之间的天数,可以使用

ChronoUnit.DAYS.between(start, end)

所以要计算从现在到一年的日期的天数,您可以使用

LocalDate today = LocalDate.now();
long days = ChronoUnit.DAYS.between(today, today.plusYears(1));

如果您想查看一年的会员是否仍然有效,您可以使用

Period membershipLength = Period.ofYears(1);
LocalDate membershipStart = ...;
LocalDate membershipEnd = membershipStart.plus(membershipLength);

LocalDate today = LocalDate.now();
boolean memberShipEnded = today.isAfter(membershipEnd);
boolean membershipValid = !membershipEnded;

很明显,您不需要持续时间(= 两个日期之间),而是特定日期的年份长度。

LocalDate dateLeap = LocalDate.of(2004, Month.MARCH, 1);
System.out.println("leap year of " + dateLeap
    + " has days: " + dateLeap.lengthOfYear());

leap year of 2004-03-01 has days: 366

Java 8 日期和时间是惊人的完整。


如果你的意思是,2004 年 1 月 5 日到 2005 年 1 月 5 日 = 366 和 2004 年 3 月 2 日到 2005 年 3 月 2 日 = 365

int lengthOfYear(LocalDate date) {
    return date.getMonthValue() <= 2
        ? date.lengthOfYear()               // Count Feb in this year
        : date.plusYears(1).lengthOfYear(); // Count Feb in next year
}

说明:基本上长度是365。但是如果日期>=三月,则计算下一年的二月,否则为今年的二月。

请注意 plusYears(1) 不会更改 DAY 或 MONTH。

此外,闰秒和 2 月 29 日的 hour/minuts 都不被考虑。