我在 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.
关于java - Joda-时间周期计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34634965/