简单易懂且实用全面的身份证校验工具类,超详细注释

public class Util {
   /**
     * 中国大陆身份证规则说明  身份证号码的格式:622301-20200716-612-X
     * 当今的身份证号码有15位和18位之分。1985年我国实行居民身份证制度,当时签发的身份证号码是15位的,1999年签发的身份证由于年份的扩展(由两位变为四位)和末尾加了效验码,就成了18位。
     *
     * 由18位数字组成:前6位为地址码,第7至14位为出生日期码,第15至17位为顺序码,
     * 第18位为校验码。检验码分别是0-10共11个数字,当检验码为“10”时,为了保证公民身份证号码18位,所以用“X”表示。虽然校验码为“X”不能更换,但若需全用数字表示,只需将18位公民身份号码转换成15位居民身份证号码,去掉第7至8位和最后1位3个数码。
     * (1)前1、2位数字表示:所在省份的代码;
     * (2)第3、4位数字表示:所在城市的代码;
     * (3)第5、6位数字表示:所在区县的代码;
     * (4)第7~14位数字表示:出生年、月、日;
     * (5)第15、16位数字表示:所在地的派出所的代码;
     * (6)第17位数字表示性别:奇数表示男性,偶数表示女性
     * (7)第18位数字是校检码:根据一定算法生成
     * 校验码算法规则:
        1. 对前17位数字本体码加权求和
           公式为:S = Sum(Ai * Wi), i = 0, ... , 16
           其中Ai表示第i个位置上的身份证号码数字值,Wi表示第i位置上的加权因子,其各位对应的值依次为: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
       2. 用11对计算结果取模
            Y = mod(S, 11)
       3. 根据模的值得到对应的校验码
           对应关系为:
            Y值:     0  1  2  3  4  5  6  7  8  9  10
            校验码: 1  0  X  9  8  7  6  5  4  3   2
     *
     * 身份证合法性校验
     *
     * @param idCard 待校验身份证号码
     * @return 校验结果,true代表合法,false代表不合法
     */
    public static boolean validateIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard)) {
            LOG.error("idCard {} is empty", idCard);
            return false;
        }

        int cardLength = idCard.length();
        // 不为15或18位直接返回false
        if (cardLength != 15 && cardLength != 18) {
            LOG.error("The idCard {} should be 15 or 18 digits", idCard);
            return false;
        }

        String card =
            cardLength == 18 ? idCard.substring(0, 17) : idCard.substring(0, 6) + 19 + idCard.substring(6, 15);

        // 对于15位身份证应该全是数字,对于18位身份证前17位应该全是数字
        if (!isNumber(card)) {
            LOG.error(
                "The first 17 digits of the 18-digit IdCard should be all Numbers, while the 15-digit IdCard should be all Numbers");
            return false;
        }

        // 出生年份
        String year = card.substring(6, 10);

        // 出生月份
        String month = card.substring(10, 12);

        // 出生日期
        String day = card.substring(12, 14);

        // 判断身份证日期是否合法
        if (!isDate(year + "-" + month + "-" + day)) {
            LOG.error("The date of birth is invalid");
            return false;
        }

        // 判断身份证日期是否在有效范围内
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 不得大于150岁或者迟于今天出生
            if ((gc.get(Calendar.YEAR) - Integer.parseInt(year)) > 150
                || (gc.getTime().getTime() - s.parse(year + "-" + month + "-" + day).getTime()) < 0) {

                LOG.error("Id date of birth is not valid");
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // 月份在1-12之间
        if (Integer.parseInt(month) > 12 || Integer.parseInt(month) == 0) {
            LOG.error("Id card month invalid");
            return false;
        }

        // 日期天数在1-31之间
        if (Integer.parseInt(day) > 31 || Integer.parseInt(day) == 0) {
            LOG.error("IdCard date invalid");
            return false;
        }

        // 判断地区码是否有效
        HashMap areaCode = getAreaCode();

        // 如果身份证前两位的地区码不在areaCode,则地区码有误
        if (!areaCode.containsKey(card.substring(0, 2))) {
            LOG.error("IdCard code invalid");
            return false;
        }

        if (!isValidCode(card, idCard)) {
            LOG.error("Id check code invalid, not a legal ID number");
            return false;
        }

        return true;
    }

    /**
     * 校验18位身份证最后一位检验码是否合规
     *
     * @param card 身份证前17位
     * @param idCard 身份证号
     * @return 校验结果,true合法,false非法
     */
    private static boolean isValidCode(String card, String idCard) {
        if (idCard.length() == 18) {
            String[] validCode = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
            String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
            int sum = 0;
            for (int i = 0; i < 17; i++) {
                sum += Integer.parseInt(String.valueOf(card.charAt(i))) * Integer.parseInt(Wi[i]);
            }
            int modValue = sum % 11;
            String strVerifyCode = validCode[modValue];
            card += strVerifyCode;
            if (!StringUtils.equals(card, idCard)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 校验字符串中是否全部位数字
     *
     * @param str 待校验字符串
     * @return 验证结果,true全是数字,false相反
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (isNum.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断字符串出生日期是否符合正则表达式:包括年月日,闰年、平年和每月31天、30天和闰月的28天或者29天
     *
     * @param strDate 待校验日期
     * @return 校验结果,true日期合法,false日期非法
     */
    private static boolean isDate(String strDate) {

        Pattern pattern = Pattern.compile(
            "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
        Matcher m = pattern.matcher(strDate);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 省地区编码对应关系
     * @return map集合对应省编码
     */
    private static HashMap getAreaCode() {
        HashMap areaCode = new HashMap();
        areaCode.put("11", "北京");
        areaCode.put("12", "天津");
        areaCode.put("13", "河北");
        areaCode.put("14", "山西");
        areaCode.put("15", "内蒙古");
        areaCode.put("21", "辽宁");
        areaCode.put("22", "吉林");
        areaCode.put("23", "黑龙江");
        areaCode.put("31", "上海");
        areaCode.put("32", "江苏");
        areaCode.put("33", "浙江");
        areaCode.put("34", "安徽");
        areaCode.put("35", "福建");
        areaCode.put("36", "江西");
        areaCode.put("37", "山东");
        areaCode.put("41", "河南");
        areaCode.put("42", "湖北");
        areaCode.put("43", "湖南");
        areaCode.put("44", "广东");
        areaCode.put("45", "广西");
        areaCode.put("46", "海南");
        areaCode.put("50", "重庆");
        areaCode.put("51", "四川");
        areaCode.put("52", "贵州");
        areaCode.put("53", "云南");
        areaCode.put("54", "西藏");
        areaCode.put("61", "陕西");
        areaCode.put("62", "甘肃");
        areaCode.put("63", "青海");
        areaCode.put("64", "宁夏");
        areaCode.put("65", "新疆");
        areaCode.put("71", "台湾");
        areaCode.put("81", "香港");
        areaCode.put("82", "澳门");
        areaCode.put("91", "国外");
        return areaCode;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值