如何在Java中计算某人的年龄?
我想在Java方法中将年限作为int返回。我现在拥有的是以下情况,其中getBirthDate()返回一个Date对象(带有出生日期;-)):
public int getAge() {
long ageInMillis = new Date().getTime() - getBirthDate().getTime();
Date age = new Date(ageInMillis);
return age.getYear();
}
但是由于getYear()被弃用了,我想知道是否有更好的方法来做到这一点?我甚至不确定这是否有效,因为我(还没有)任何单元测试。