根据用户出生日期计算年龄
public static int calculateAgeFromDate(Date birthDate) {
// 将 Date 转换为 LocalDate(默认时区)
LocalDate birthLocalDate = birthDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
// 获取当前日期LocalDate currentDate = LocalDate.now();// 计算基础年龄差值int age = Period.between(birthLocalDate, currentDate).getYears();// 如果当前日期未过生日,年龄减 1if (currentDate.getMonthValue() < birthLocalDate.getMonthValue() ||(currentDate.getMonthValue() == birthLocalDate.getMonthValue() &¤tDate.getDayOfMonth() < birthLocalDate.getDayOfMonth())) {age--;}if (age < 0){return 0;}return age;
}