Joda-Time 周期计算
Joda-Time Period Calculation
我在 Joda-Time 计算某人年龄的周期计算中有奇怪的行为。如果某人出生于 1970 年 1 月 5 日,那么截至今天他们应该是 46 岁零 1 天。但是,如果我使用以下年龄计算:
LocalDate birthdate = new LocalDate(1970,1,5);
LocalDate today = new LocalDate();
Period period = new Period(birthdate, today, PeriodType.yearMonthDay());
int age = period.getYears();
并且 joda-time 说周期是 P45Y1D
。我已经在 wolfram 上尝试过这个来验证我没有疯并且它同意我的观点。 joda-time 在这里做了什么导致了不同的结果?
我认为这是因为您的计算机时钟设置不正确。硬编码日期如下产生预期结果:
LocalDate birthdate = new LocalDate(1970,1,5);
LocalDate today = new LocalDate(2016, 1, 6); // Rather than relying on system clock.
Period period = new Period(birthdate, today, PeriodType.yearMonthDay());
int age = period.getYears(); // 46.
我在 Joda-Time 计算某人年龄的周期计算中有奇怪的行为。如果某人出生于 1970 年 1 月 5 日,那么截至今天他们应该是 46 岁零 1 天。但是,如果我使用以下年龄计算:
LocalDate birthdate = new LocalDate(1970,1,5);
LocalDate today = new LocalDate();
Period period = new Period(birthdate, today, PeriodType.yearMonthDay());
int age = period.getYears();
并且 joda-time 说周期是 P45Y1D
。我已经在 wolfram 上尝试过这个来验证我没有疯并且它同意我的观点。 joda-time 在这里做了什么导致了不同的结果?
我认为这是因为您的计算机时钟设置不正确。硬编码日期如下产生预期结果:
LocalDate birthdate = new LocalDate(1970,1,5);
LocalDate today = new LocalDate(2016, 1, 6); // Rather than relying on system clock.
Period period = new Period(birthdate, today, PeriodType.yearMonthDay());
int age = period.getYears(); // 46.