根据出生日期算当前年龄

This commit is contained in:
liuhongrui
2025-02-24 19:38:29 +08:00
parent 7e5f3614fa
commit 04d61916a5
2 changed files with 83 additions and 1 deletions

View File

@@ -0,0 +1,82 @@
package com.core.common.utils;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
/**
* 根据出生日期算年龄
*
* @author liuhr
* @date 2025/2/24
*/
public final class AgeCalculatorUtil {
/**
* 当前年龄取得(床位列表表示年龄用)
*/
public static String getAge(LocalDateTime dateTime) {
LocalDateTime now = LocalDateTime.now();
int years = now.getYear() - dateTime.getYear();
if (years > 2) {
return String.format("%d岁", years);
}
Period period = Period.between(dateTime.toLocalDate(), now.toLocalDate());
int months = period.getMonths();
int days = period.getDays();
long hours = ChronoUnit.HOURS.between(dateTime, now) - (days * 24);
if (hours < 0) {
hours += 24;
days--;
}
if (days < 0) {
months--;
days = getLastDayOfMonth(dateTime) - dateTime.getDayOfMonth() + now.getDayOfMonth();
}
if (months < 0) {
months += 12;
years--;
}
if (years < 0) {
return "1小时";
}
if (years > 0 && months > 0) {
return String.format("%d岁%d月", years, months);
}
if (years > 0) {
return String.format("%d岁", years);
}
if (months > 0 && days > 0) {
return String.format("%d月%d天", months, days);
}
if (months > 0) {
return String.format("%d月", months);
}
if (days > 0 && hours > 0) {
return String.format("%d天%d小时", days, hours);
}
if (days > 0) {
return String.format("%d天", days);
}
if (hours > 0) {
return String.format("%d小时", hours);
}
return "1小时";
}
private static int getLastDayOfMonth(LocalDateTime dateTime) {
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(dateTime.getYear()) && dateTime.getMonthValue() == 2) {
return 29;
}
return daysInMonth[dateTime.getMonthValue()];
}
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}

View File

@@ -8,7 +8,7 @@ import net.sourceforge.pinyin4j.PinyinHelper;
* @author liuhr * @author liuhr
* @date 2025/2/24 * @date 2025/2/24
*/ */
public final class ChineseConvert { public final class ChineseConvertUtils {
/** /**
* 将中文汉字转换为首字母拼音 * 将中文汉字转换为首字母拼音