我必须在这里做一些根本错误的事情。我有非常简单的代码:
private static final long MILLIS_PER_YEAR = 1000 * 60 * 60 * 24 * 365;
//...
public long getAge() {
long millis = System.currentTimeMillis() - this.getBirthdate().getTime();
System.out.println("Computed age: " + (millis / MILLIS_PER_YEAR) + ", birthdate=" + this.getBirthdate() + ", birthdateMillis="
+ this.getBirthdate().getTime() + ", now=" + new Date() + ", nowMillis=" + System.currentTimeMillis()
+ ", elapsedMillis=" + millis);
return millis / MILLIS_PER_YEAR;
}
...但它给出了一些完全不正确的输出:
Computed age: 248, birthdate=2001-01-01 10:00:00.0, birthdateMillis=978307200000, now=Fri Aug 10 16:56:48 EST 2012, nowMillis=1344581808173, elapsedMillis=366274608173
Computed age: 184, birthdate=2004-01-01 10:00:00.0, birthdateMillis=1072915200000, now=Fri Aug 10 16:56:48 EST 2012, nowMillis=1344581808173, elapsedMillis=271666608173
如果我手动(或使用 Google)运行相同的计算,我会得到正确的结果(在合理的范围内,因为实际一年中的天数略多于 365 天)。
同样的数学是如何在这段代码中产生如此荒谬的输出的?