使用 java.time.LocalDate 计算从生日起到现在年龄的方法。
1 2 3 4 5 6 7 8 9 10 11 12
| public int getAge(int year, int month, int day) {
LocalDate birthday = LocalDate.of(year, month, day);
LocalDate today = LocalDate.now();
long duration = ChronoUnit.YEARS.between(birthday, today);
return (int)duration; }
|