好像没有现成的工具类提供,找AI写了个方法
public static String formatBirthday(String birthday) {
// 解析出生日期(只取月日)
LocalDate birthDate;
try {
birthDate = LocalDate.parse(birthday);
} catch (DateTimeParseException e) {
return birthday;
}
LocalDate today = LocalDate.now();
// 构造今年的生日
LocalDate thisYearBirthday = LocalDate.of(today.getYear(), birthDate.getMonth(), birthDate.getDayOfMonth());
// 如果今年生日已过,考虑明年
if (thisYearBirthday.isBefore(today)) {
thisYearBirthday = thisYearBirthday.plusYears(1);
}
// 计算相差天数
long daysUntil = ChronoUnit.DAYS.between(today, thisYearBirthday);
if (daysUntil == 0) {
return "今天";
} else if (daysUntil == 1) {
return "明天";
} else if (daysUntil <= 7) {
return daysUntil + "天后";
} else {
return thisYearBirthday.format(DateTimeFormatter.ofPattern("M月d日"));
}
}