Java版完整身份证号校验工具类

支持中国大陆的身份证号码校验,包括新版18位身份证和老版的15位身份证规则校验

具体代码可直接到GitHub拉取源代码:https://github.com/bj4096/common-utils

如有更好的建议欢迎大家随时提交,共同完善

具体的身份证号码规则说明请自行到网上搜索,或参见第二代身份证号码编排规则

废话不多说,直接上代码:

public static void main(String[] args) {
        System.out.println(IdCardUtil.checkIdCard(身份证号));
    }
/**
 * 身份证号码校验工具类
 *
 * @author L.Y.F
 * @create 2019-07-23 18:35
 */
public class IdCardUtil {

    private static Map regionCodeMap = Maps.newHashMap();
    // 每位加权因子
    private static int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    // 第18位校检码
    private static String verifyCode[] = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};


    private static final String reg18Bitb = "\\d{6}(18|19|([2]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
    private static final String reg15Bitb = "\\d{6}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}";

    public static boolean check18(String idCard) {
        if (StringUtils.isNotBlank(idCard)) {
            Pattern regexp = Pattern.compile(reg18Bitb);
            if (regexp.matcher(idCard).matches()) {
                return validate18Idcard(idCard);
            }
        }
        return false;
    }

    public static boolean check15(String idCard) {
        if (StringUtils.isNotBlank(idCard)) {
            Pattern regexp = Pattern.compile(reg15Bitb);
            if (regexp.matcher(idCard).matches()) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkIdCard(String idCard) {
        if (StringUtils.isNotBlank(idCard)) {
            if (check18(idCard) || check15(idCard)) {
                // String region = idCard.substring(0, 6);
                // if (regionCodeMap.containsKey(region)) {
                    return true;
                // }
            }
        }
        return false;
    }

    private static boolean validate18Idcard(String idCard) {
        // 获取前17位
        String idcard17 = idCard.substring(0, 17);
        // 获取第18位
        String idcard18Code = idCard.substring(17, 18);
        int[] IDnums = new int[17];
        for (int i = 0; i < idcard17.length(); i++) {
            IDnums[i] = idcard17.charAt(i) - '0';
        }
        int sum = getSum(IDnums, power);
        int last = getLastNum(sum);
        String lastStr = (last == 10 ? "X" : last+"");
        if (!idcard18Code.equalsIgnoreCase(lastStr)) {
            return false;
        }
        return true;
    }

    private static int getSum(int IDnums[], int coe[]) {
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += IDnums[i] * coe[i];
        }
        return sum;
    }

    private static int getLastNum(int num) {
        num = num % 11;
        switch (num) {
            case 0:
                num = 1;
                break;
            case 1:
                num = 0;
                break;
            case 2:
                num = 10;
                break;
            case 3:
                num = 9;
                break;
            case 4:
                num = 8;
                break;
            case 5:
                num = 7;
                break;
            case 6:
                num = 6;
                break;
            case 7:
                num = 5;
                break;
            case 8:
                num = 4;
                break;
            case 9:
                num = 3;
                break;
            case 10:
                num = 2;
                break;
        }
        return num;
    }

    public static void main(String[] args) {
        String idcard17 = "12011219810129216";
        int[] IDnums = new int[17];
        for (int i = 0; i < idcard17.length(); i++) {
            IDnums[i] = idcard17.charAt(i) - '0';
        }
        int sum = getSum(IDnums, power);
        int last = getLastNum(sum);
        String idCard = idcard17+last;
        System.out.println(idcard17+last);
        System.out.println(IdCardUtil.checkIdCard(idCard));
    }

}

你可能感兴趣的:(Java版完整身份证号校验工具类)