我想找一个人的年龄-考虑到他的生日(年,月,日)。我怎样才能用ThreeTenBP做到这一点呢?
编辑:我找到的一个选择是这样
LocalDate birthdate = LocalDate.of(year, month, day);
return (int) birthdate.until(LocalDate.now(), ChronoUnit.YEARS);发布于 2016-04-12 22:37:28
你的代码在大多数国家都是正确的。它假设2月29日出生的人在非闰年的3月1日过生日。读维基百科。
然而,这使得ThreeTen与自身不一致,不像Joda-time。
// if birthdate = LocalDate.of(2012, Month.FEBRUARY, 29);
System.out.println (birthdate.until(birthdate.plusYears(1), ChronoUnit.YEARS)); // display 0如果希望getAge()与plusYears()同步,可以编写:
public static int getAge(LocalDate birthdate, LocalDate today)
{
int age = today.getYears() - birthdate.getYears();
if (birthdate.plusYears(age).isAfter(today))
age--;
return age;
}https://stackoverflow.com/questions/31482267
复制相似问题