新增工具类生日年龄转换方法类

This commit is contained in:
tangw 2025-07-27 11:48:57 +08:00
parent 65a0ae3687
commit d66ce72b8c

View File

@ -17,8 +17,8 @@ public class AgeUtils {
private static final double HOURS_PER_YEAR = 365.25 * 24; // 8766.0
// 常量:时间单位(支持岁、月、天)
private static final String UNIT_YEAR = "岁";
private static final String UNIT_MONTH = "个月";
private static final String UNIT_MONTH0 = "月";
private static final String UNIT_MONTH = "月";
private static final String UNIT_MONTH0 = "个月";
private static final String UNIT_DAY = "天";
private static final String UNIT_HOUR= "时";
private static final String UNIT_HOUR0= "小时";
@ -212,7 +212,7 @@ public class AgeUtils {
// 1. 计算总毫秒差
long diffMillis = currentCal.getTimeInMillis() - birthCal.getTimeInMillis();
if (diffMillis <= 0) {
result.put("nldw", "岁");
result.put("nldw", UNIT_YEAR);
result.put("nl", 0);
return result;
}
@ -221,14 +221,14 @@ public class AgeUtils {
long totalHours = diffMillis / (1000 * 60 * 60); // 总小时数
if (totalHours < 24) { // 不足1天:返回小时
result.put("nldw", "时");
result.put("nldw", UNIT_HOUR);
result.put("nl", (int) totalHours);
return result;
}
long totalDays = totalHours / 24; // 总天数
if (totalDays < 30) { // 不足1月:返回天数
result.put("nldw", "天");
result.put("nldw", UNIT_DAY);
result.put("nl", (int) totalDays);
return result;
}
@ -249,7 +249,7 @@ public class AgeUtils {
}
if (months < 12) { // 不足1岁:返回月数
result.put("nldw", "月");
result.put("nldw", UNIT_MONTH);
result.put("nl", months);
return result;
}
@ -260,7 +260,7 @@ public class AgeUtils {
years--;
}
result.put("nldw", "岁");
result.put("nldw", UNIT_YEAR);
result.put("nl", years);
return result;
}