身份证校验,真实版,屏蔽了地区校验

去掉了地区验证

package packer;



import java.util.Calendar;

public class IdNumberValidate {

    public String  idValidate( String idNumber) {
        String result="非法身份证";
        if (!checkLength(idNumber)) {
            return result;
        }

        if (!checkIllegalChar(idNumber)) {
            return result;
        }

       /* if (!checkRegion(idNumber, refData)) {
            return result;
        }*/

        if (!checkBirthday(idNumber)) {
            return result;
        }

        if (!checkParityBit(idNumber)) {
            return result;
        }

       return "标准身份证";
    }

    static boolean checkLength(String idNumber) {
        return idNumber.length() == 15 || idNumber.length() == 18;
    }

    static boolean checkIllegalChar(String idNumber) {
        if (idNumber.matches("[0-9]{15}") || idNumber.matches("[0-9]{17}[0-9|X]")) {
            return true;
        }
        return false;
    }

    /*static boolean checkRegion(String idNumber, IdNumberRefData refData) {
        String region = idNumber.substring(0, 6);
        Map<String, String> regions = refData.getRegions();
        return regions.get(region) != null;
    }*/

    static boolean checkBirthday(String idNumber) {
       Calendar calendar = Calendar.getInstance();
      int currentYear = calendar.get(Calendar.YEAR);
      int currentMonth = calendar.get(Calendar.MONTH)+1;
      int currentDay= calendar.get(Calendar.DAY_OF_MONTH);
        int year;
        int month;
        int day;
        if (idNumber.length() == 15) {
            year = Integer.parseInt(idNumber.substring(6, 8)) + 1900;
            month = Integer.parseInt(idNumber.substring(8, 10));
            day = Integer.parseInt(idNumber.substring(10, 12));
        } else {
            year = Integer.parseInt(idNumber.substring(6, 10));
            month = Integer.parseInt(idNumber.substring(10, 12));
            day = Integer.parseInt(idNumber.substring(12, 14));
        }
        if (year < 1900 || year > currentYear) {
            return false;
        }
        if( year == currentYear){
           if(month > currentMonth){
               return false;
           }else if(month==currentMonth) {
              if (day > currentDay) {
                 return false;
              }
         }
        }

        if (month < 1 || month > 12) {
            return false;
        }

        boolean isLeapYear = year % (year % 100 == 0 ? 400 : 4) == 0;
        if (day < 1 || day > 31) {
            return false;
        }
        if (month == 2 && day > (isLeapYear ? 29 : 28)) {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
            return false;
        }
        return true;
    }

    static boolean checkParityBit(String idNumber) {
        if (idNumber.length() == 18) {
            int[] weights = {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 += weights[i] * (idNumber.charAt(i) - 48);
            }
            char[] arr = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
            int remainder = sum % 11;
            if (idNumber.charAt(17) != arr[remainder]) {
                return false;
            }
        }
        return true;
    }
}

 

 

package packer;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Hello {
    public static void main(String[] agrs) throws IOException {

        System.out.println( );

        System.out.println( );
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "gbk"));
        String line = null;
        while (true) {
            System.out.println("***输入身份证号码查询此人的年龄状态,9月以后的默认加一,9月以前的即时满了一岁,也是减一岁**");
            System.out.println("**例如2011年8月31日,由于是9月以前出生的,假如现在是2018年9月一号,实际年龄是7岁,这里算6岁**");
            System.out.println("**例如2011年9月1日,由于是9月以后出生的,假如现在是2018一月一号,实际年龄是6岁,这里算7岁**");
            System.out.println("输入身份证号码(输入exit退出或者ctrl+c):");
            line = br.readLine();
            if (line.equals("exit")) {
                break;
            }
            IdNumberValidate idNumberValidate=new IdNumberValidate();
            idNumberValidate.idValidate(line);
            if (line.length()<14&&line.length()<19){
                System.out.println("请输入正确的身份证号码");
                System.out.println("                                                            ");
            }else{
                String month = line.substring(10, 12);
                int result = Integer.parseInt(month);
                System.out.println(line+"的证件号码的年龄是:"+BirthdayUntil .getAgeId(line)+",出生月份是:"+result+"月");
                System.out.println(line+"的证件号码的年龄状态是:"+BirthdayUntil .getAge(BirthdayUntil .getAgeId(line),result));
                System.out.println("                                                            ");
            }

        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值