枚举身份证后四位java代码

该代码实现了一个Java程序,用于生成所有可能的18位身份证号后四位组合,并使用身份证号验证工具类进行校验。校验过程包括检查生日、奇偶性和校验位。同时,提供了将15位身份证号升级到18位以及还原18位身份证号到15位的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

枚举身份证后四位java代码

class Test {
    public static void main(String[] args) {
        char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X'};
        ArrayList<String> list = new ArrayList<>();
        char[] id = "xxxxxxxxxxxxxx0000".toCharArray();
        for (int i = 0; i < arr.length - 1; i++) {
            //顺序位
            id[14] = arr[i];
            for (int i1 = 0; i1 < arr.length - 1; i1++) {
                //顺序位
                id[15] = arr[i1];
                for (int i2 = 0; i2 < arr.length - 1; i2++) {
                    //性别位 奇数为男 偶数为女
                    if (Integer.parseInt(arr[i2]+"") % 2 == 0) {
                        id[16] = arr[i2];
                        for (int i3 = 0; i3 < arr.length; i3++) {
                            //检验位
                            id[17] = arr[i3];
                            String temp = String.valueOf(id);
                            if (IdentificationCodeUtil.isIdentityCode(temp)) {
                                System.out.println(temp);
                                list.add(temp);
                            }
                        }
                    }

                }
            }

        }
        System.out.println(list.size());
        System.out.println(list.get(0));
    }
/**
 * 身份证号工具类
 *
 */
static class IdentificationCodeUtil {

    public static final int IDENTITYCODE_OLD = 15; // 老身份证15位
    public static final int IDENTITYCODE_NEW = 18; // 新身份证18位
    public static int[] Wi = new int[17];

    /**
     * 判断身份证号码是否正确。
     *
     * @param code
     *            身份证号码。
     * @return 如果身份证号码正确,则返回true,否则返回false。
     */

    public static boolean isIdentityCode(String code) {

        if (code == null || "".equals(code.trim())) {
            return false;
        }

        String birthDay = "";
        code = code.trim();

        // 长度只有15和18两种情况
        if ((code.length() != IDENTITYCODE_OLD)
                && (code.length() != IDENTITYCODE_NEW)) {
            return false;
        }

        // 身份证号码必须为数字(18位的新身份证最后一位可以是x)
        Pattern pt = Pattern.compile("\\d{15,17}([\\dxX]{1})?");
        Matcher mt = pt.matcher(code);
        if (!mt.find()) {
            return false;
        }

        // 验证生日
        if (code.length() == IDENTITYCODE_OLD) {
            birthDay = "19" + code.substring(6, 12);
        } else {
            birthDay = code.substring(6, 14);
        }
        try {
            new SimpleDateFormat("yyyyMMdd").parse(birthDay);
        } catch (ParseException e) {
            return false;
        }
//        if (!TimeUtil.isRightDate(birthDay, "yyyyMMdd")) {
//            return false;
//        }

        // 最后一位校验码验证
        if (code.length() == IDENTITYCODE_NEW) {
            String lastNum = getCheckFlag(code.substring(0,
                    IDENTITYCODE_NEW - 1));
            // check last digit
            if (!("" + code.charAt(IDENTITYCODE_NEW - 1)).toUpperCase().equals(
                    lastNum)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 获取新身份证的最后一位:检验位
     *
     * @param code
     *            18位身份证的前17位
     * @return 新身份证的最后一位
     */
    private static String getCheckFlag(String code) {

        int[] varArray = new int[code.length()];
        String lastNum = "";
        int numSum = 0;
        // 初始化位权值
        setWiBuffer();
        for (int i = 0; i < code.length(); i++) {
            varArray[i] = new Integer("" + code.charAt(i)).intValue();
            varArray[i] = varArray[i] * Wi[i];
            numSum = numSum + varArray[i];
        }
        int checkDigit = 12 - numSum % 11;
        switch (checkDigit) {
            case 10:
                lastNum = "X";
                break;
            case 11:
                lastNum = "0";
                break;
            case 12:
                lastNum = "1";
                break;
            default:
                lastNum = String.valueOf(checkDigit);
        }
        return lastNum;
    }

    /**
     * 初始化位权值
     */
    private static void setWiBuffer() {
        for (int i = 0; i < Wi.length; i++) {
            int k = (int) Math.pow(2, (Wi.length - i));
            Wi[i] = k % 11;
        }
    }

    /**
     * 判别是否字符串为null或者没有内容,或者全部为空格。
     */
    public static boolean empty(String o) {
        return ((null == o) || (o.length() <= 0) || (o.trim().equals("")));
    }

    /**
     * 将15位身份证号码升级为18位身份证号码
     *
     * @param code
     *            15位身份证号码
     * @return 18位身份证号码
     */
    public static String update2eighteen(String code) {

        if (code == null || "".equals(code.trim())) {
            return "";
        }

        code = code.trim();

        if (code.length() != IDENTITYCODE_OLD || !isIdentityCode(code)) {
            return "";
        }

        code = code.substring(0, 6) + "19" + code.substring(6);
        //
        code = code + getCheckFlag(code);

        return code;
    }

    /**
     * 还原15位身份证号码
     * @param code
     * @return
     */
    public  static String resume2fifteen(String code){

        if (code == null || "".equals(code.trim())) {
            return "";
        }

        code = code.trim();

//        if (code.length() != IDENTITYCODE_NEW || !isIdentityCode(code)) {
//            return "";
//        }

        if (code.length() != IDENTITYCODE_NEW ) {
            return "";
        }
        StringBuffer codebuffer = new StringBuffer(code);
        codebuffer.delete(6, 8);
        codebuffer.deleteCharAt(codebuffer.length() -1 );

        return codebuffer.toString();
    }

    public static void main(String[] args){
        String[] codeArray = new String[]{"62272219850510261x","53010119810602007x","53010119810602001x"};
        for(int i= 0;i<codeArray.length;i++){
            if(isIdentityCode(codeArray[i])){
                System.out.println(codeArray[i]+":为正确的身份证号码!");
            }else{
                System.out.println(codeArray[i]+":为错误的身份证号码!");
            }
        }

        System.out.println("转换后的身份证号码为:"+update2eighteen("330521820721052"));;
        System.out.println("转换后的身份证号码为:"+resume2fifteen("62272219850510261x"));;

    }


}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值