使用LocalDate来计算年龄

2018-08-03  本文已影响0人  温泉的进击

使用 java.time.LocalDate 计算从生日起到现在年龄的方法。

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;
}
上一篇 下一篇

猜你喜欢

热点阅读