java身份证校验工具,国标 拒绝花里胡哨的验证

直接上代码,规则来自百度百科


public class IdCardCheckUtils {
    //身份证前17位数字依次乘以对应的权重因子
    public static final Integer[] idCardWeight= {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    //身份证最后一位对应的校验码
    public static final String[] idCardCheck= {"1","0","X","9","8","7","6","5","4","3","2"};

    //星座数组
    public static final String[] CONSTELLATION_ARR = { "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };
    //星座对应的边缘日期
    public static final int[] CONSTELLATION_EDGE_DAY = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};

    //生肖
    public static final String[] ZODIAC_ARR = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊" };

    //组装根据余数,对应一个指定的校验码
    Map<Integer,String> idCardMap=new HashMap<>();

    public Boolean idCardNumberCheck(String idCardNo){
        boolean check=false;
        //将其转成大写有的身份证最后一位是字母
        String idCard=idCardNo.toUpperCase();

        //15位身份证转成18位
//        Integer verifyNum=0;
        if (idCardNo.length() == 15) {
            if (!(idCardNo.matches( "[0-9]{17}[0-9|x]|[0-9]{15}"))) {
                System.out.println("请输入正确格式的15位身份证号码");
                return false;
            } else {
                //15位转换为18位
                String s2 = idCardNo.substring(0, 6);
                String s3 = idCardNo.substring(6, 15);
                String changed = s2.concat("19").concat(s3);
                idCard=changed.toUpperCase();
            }
        }
        //获取身份证最后一位进行验证
        String lastStr = idCard.substring(idCard.length() - 1);
        //获取身份证前17位
        String firstStr = idCard.substring(0,17);
        //验证身份证前17位是否为数字
        boolean isDigits = Pattern.matches("^\\d{17}", firstStr);
        if(!isDigits){
            return false;
        }
        //全局变量
        if(idCardMap.isEmpty()){
            for(int i=0;i<=10;i++){
                idCardMap.put(i,idCardCheck[i]);
            }
        }

        char[] idCardCharNumber=idCard.toCharArray();
        Integer resultSum=0;
        for(int i=0;i< idCardCharNumber.length-1;i++){
            resultSum+=Character.getNumericValue(idCardCharNumber[i])*idCardWeight[i];
        }
        //将相加的前17位数字依次乘以对应的权重因子相加,相加的结果除以11,得到余数
        Integer lastResult=resultSum%11;
        //根据余数,对应一个指定的校验码。最终得到的校验码就是身份证号码的最后一位数字。通过这个校验码,可以验证前面17位数字是否正确,从而提高身份证号码的准确性
        if(idCardMap.containsKey(lastResult)){
            check = idCardMap.get(lastResult).equals(lastStr);
        }
        return check;
    }

    public static void main(String[] args) throws Exception {
        IdCardCheckUtils test = new IdCardCheckUtils();
        String idCard="532530198101201272";
        Boolean idCardNumberCheck = test.idCardNumberCheck(idCard);
        if(!idCardNumberCheck){
            System.out.println("身份证号码有误!");
            System.exit(0);
        }else {
            System.out.println("身份证号格式正确");}
    }

    /**
     * 根据日期获取星座
     * @return
     */
    public static String getConstellation(Date date) {
        if (date == null) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (day < CONSTELLATION_EDGE_DAY[month]) {
            month = month - 1;
        }
        if (month >= 0) {
            return CONSTELLATION_ARR[month];
        }
        // default to return 魔羯
        return CONSTELLATION_ARR[11];
    }

    /**
     *  根据身份证号判断用户星座
     * @param cardNo
     * @return
     */
    public static String getConstellation(String cardNo) {
        // 获取出生日期
        String birthday = cardNo.substring(6, 14);
        Date birthdate = null;
        try {
            birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
            return getConstellation(birthdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据日期获取生肖
     * @return
     */
    public static String getZodica(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return ZODIAC_ARR[cal.get(Calendar.YEAR) % 12];
    }

    /**
     *  根据身份证号判断用户生肖
     * @param cardNo
     * @return
     */
    public static String getZodica(String cardNo) {
        // 获取出生日期
        String birthday = cardNo.substring(6, 14);
        Date birthdate = null;
        try {
            birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
            return getZodica(birthdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 15位转18位身份证号
     * @param identityCard
     * @return
     */
    public static String get18Ic(String identityCard) {
        String retId = "";
        String id17 = "";
        int sum = 0;
        int y = 0;
        // 定义数组存放加权因子(weight factor)
        int[] wf = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        // 定义数组存放校验码(check code)
        String[] cc = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
        if (identityCard.length() != 15) {
            return identityCard;
        }
        // 加上两位年19
        id17 = identityCard.substring(0, 6) + "19" + identityCard.substring(6);
        // 十七位数字本体码加权求和
        for (int i = 0; i < 17; i++) {
            sum = sum + Integer.valueOf(id17.substring(i, i + 1)) * wf[i];
        }
        // 计算模
        y = sum % 11;
        // 通过模得到对应的校验码 cc[y]
        retId = id17 + cc[y];
        return retId;
    }
}

依据!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北凉军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值