Localdate计算年龄

Posted by zjh on December 8, 2021
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;
}