chengf
2025-07-22 b63f41b8a0cfce68770e5cb02c8dd3fd994f0c59
java110-utils/src/main/java/com/java110/utils/util/CommonUtil.java
old mode 100644 new mode 100755
@@ -4,6 +4,9 @@
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
@@ -56,7 +59,7 @@
        String result = "";
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
            result += (random.nextInt(9) + 1);;
        }
        return result;
@@ -95,4 +98,30 @@
        }
        return false;
    }
    /**
     * 根据身份证号获取年龄
     * @param certId
     * @return
     */
    public static String getAgeByCertId(String certId) {
        String birthday = "";
        if (certId.length() == 18) {
            birthday = certId.substring(6, 10) + "/"
                    + certId.substring(10, 12) + "/"
                    + certId.substring(12, 14);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date now = new Date();
        Date birth = new Date();
        try {
            birth = sdf.parse(birthday);
        } catch (ParseException e) {
        }
        long intervalMilli = now.getTime() - birth.getTime();
        int age = (int) (intervalMilli/(24 * 60 * 60 * 1000))/365;
        System.out.println(age);
        return age +"";
    }
}