java根据出生日期计算年龄
public static String getAgeNow(Date birth) {
if(Objects.isNull(birth)){
return "0岁0月0天";
}
// 解析日期字符串为 LocalDate 对象
LocalDate birthDate = birth.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 计算年龄
Period age = Period.between(birthDate, currentDate);
// 提取年龄中的年、月、日
int years = age.getYears();
int months = age.getMonths();
int days = age.getDays();
return years+"岁"+months+"月零"+days+"天";
}