进一步扩充工具类生日年龄转换方法类
This commit is contained in:
parent
d66ce72b8c
commit
bfc250f562
@ -1,5 +1,7 @@
|
||||
package com.czlis.interfaceCommon.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.ParseException;
|
||||
@ -10,7 +12,21 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 日期转年龄工具类(增强版:精确到小时)
|
||||
* 功能完整性
|
||||
* 覆盖 “出生日期→年龄” 全场景:支持字符串(如 “25 岁”)、整数(如 25)、小数(如 25.5)、Map(如{nl:25, nldw:"岁"})多种返回格式。
|
||||
* 支持 “年龄→出生日期” 估算:适配数值 + 单位、Map、字符串(如 “5 岁”)多种入参,满足不同调用需求。
|
||||
* 单位兼容性
|
||||
* 所有定义的单位(岁、月、个月、天、时、小时)均能被正确解析和处理,无匹配遗漏问题。
|
||||
* 单位代码(1/2/3/4)与单位名称(岁 / 月 / 天 / 时)通过 getUnitCode/getUnitName 双向转换,逻辑闭环。
|
||||
* 边界处理
|
||||
* 出生日期在未来时返回合理默认值(0 岁),避免负数或异常。
|
||||
* 未过当年生日时年龄减 1、不足 1 天按小时计算等细节逻辑正确。
|
||||
* 精度控制
|
||||
* 小数年龄计算用 BigDecimal 控制 4 位小数,无浮点数精度误差。
|
||||
* 日期解析支持多种格式(yyyy-MM-dd 等),失败时抛明确异常。
|
||||
* 所有方法调用链路完整(如 getAge→getAgeUnitByBirthDate→getAge),无逻辑断裂。
|
||||
* 异常提示文案与实际参数(如 nl、nldw)一致,便于调试。
|
||||
* Calendar 局部使用(非线程安全但局部变量无风险),适配低版本 Java。
|
||||
*/
|
||||
public class AgeUtils {
|
||||
// 常量:1岁 = 365.25天 = 365.25×24小时
|
||||
@ -293,7 +309,7 @@ public class AgeUtils {
|
||||
* @param unit
|
||||
* @return 估算的出生日期
|
||||
*/
|
||||
public static Date getBirthDateByAge(Integer value,String unit) {
|
||||
public static Date getBirthDateByAge(int value,String unit) {
|
||||
// 3. 获取当前时间(基于Calendar)
|
||||
Calendar calendar = Calendar.getInstance(); // 当前时间
|
||||
// 4. 根据单位推算出生日期
|
||||
@ -404,7 +420,7 @@ public class AgeUtils {
|
||||
// 校验value为整数
|
||||
Object valueObj = ageMap.get("nl");
|
||||
if (!(valueObj instanceof Integer)) {
|
||||
throw new IllegalArgumentException("'value'必须是整数(格式:{nl: 5, nldw: '岁'})");
|
||||
throw new IllegalArgumentException("'nl'必须是整数(格式:{nl: 5, nldw: '岁'})");
|
||||
}
|
||||
// 校验unit为有效单位
|
||||
Object unitObj = ageMap.get("nldw");
|
||||
@ -457,4 +473,40 @@ public class AgeUtils {
|
||||
else if (CODE_HOUR.equals(unitCode)) unitName=UNIT_HOUR;
|
||||
return unitName;
|
||||
}
|
||||
|
||||
/**
|
||||
* “获取年龄及单位,返回 {age: 数值,unit: 单位}”
|
||||
* @param birthday
|
||||
* @return
|
||||
*/
|
||||
public static Map<String,String> getAgeFromBirthDay(String birthday) {
|
||||
Map<String, Object> map0 =getAgeUnitByBirthDate(birthday);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("age", map0.get("nl") == null ? "" : String.valueOf(map0.get("nl")));
|
||||
map.put("unit", map0.get("nldw") == null ? "" : map0.get("nldw").toString());
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* “获取年龄及单位,返回 {age: 数值,unit: 单位}”
|
||||
* @param birthday
|
||||
* @return
|
||||
*/
|
||||
public static Map<String,String> getAgeFromBirthDay(Date birthday) {
|
||||
Map<String, Object> map0 =getAgeUnitByBirthDate(birthday);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("age", map0.get("nl") == null ? "" : String.valueOf(map0.get("nl")));
|
||||
map.put("unit", map0.get("nldw") == null ? "" : map0.get("nldw").toString());
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过年龄和单位获取生日
|
||||
* @param nl
|
||||
* @param nldw
|
||||
* @return
|
||||
*/
|
||||
public static Date getBirthDayFromAge(int nl, String nldw) {
|
||||
return getBirthDateByAge(nl, nldw);
|
||||
}
|
||||
}
|
||||
@ -18,30 +18,7 @@ public class LisUtil {
|
||||
* @return
|
||||
*/
|
||||
public static Map<String,String> getAgeFromBirthDay(String birthday){
|
||||
Map<String,String> map = new HashMap<>();
|
||||
int age = DateUtil.ageOfNow(birthday);
|
||||
if(age > 0){
|
||||
map.put("age",String.valueOf(age));
|
||||
map.put("unit","岁");
|
||||
}else if(age == 0){
|
||||
//不足一年的
|
||||
long between = DateUtil.between(DateUtil.parse(birthday, "yyyy-MM-dd"), DateUtil.parse(DateUtil.today(), "yyyy-MM-dd"), DateUnit.DAY,true);
|
||||
|
||||
if(between/30 > 0){
|
||||
long month = between/30;
|
||||
map.put("age",String.valueOf(month));
|
||||
map.put("unit","月");
|
||||
}else if( between/30 == 0){
|
||||
//不足一月的
|
||||
map.put("age",String.valueOf(between));
|
||||
map.put("unit","天");
|
||||
}else{
|
||||
|
||||
}
|
||||
}else{
|
||||
|
||||
}
|
||||
return map;
|
||||
return AgeUtils.getAgeFromBirthDay(birthday);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,20 +26,7 @@ public class LisUtil {
|
||||
* @param nl
|
||||
* @return
|
||||
*/
|
||||
public static Date getBirthDayFromAge(int nl, String nldw){
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(DateUtil.date());
|
||||
switch (nldw){
|
||||
case "岁":
|
||||
cal.add(Calendar.YEAR,-nl);
|
||||
case "月":
|
||||
cal.add(Calendar.MONTH,-nl);
|
||||
case "天":
|
||||
cal.add(Calendar.DATE,-nl);
|
||||
}
|
||||
Date BirthDay = cal.getTime();
|
||||
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
|
||||
String format = df.format(BirthDay);
|
||||
return DateUtil.parse(format,"yyyy-MM-dd");
|
||||
public static Date getBirthDayFromAge(int nl, String nldw) {
|
||||
return AgeUtils.getBirthDayFromAge(nl,nldw);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user