2025-07-23 14:19:57 +08:00

69 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.czlis.interfaceCommon.utils;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class LisUtil {
/**
* 生日格式为:yyyy-MM-dd
* 根据出生日期计算年龄
* @param birthday
* @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;
}
/**
* 根据年龄获取出生日期
* @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");
}
}