Java:15位或18位居民身份证号码通用校验(正则表达式、日期格式、末尾校验码)

身份证号码校验,正则表达式校验、日期格式校验、18位身份证末尾校验码校验

前六位省市县号码变更频繁,这里就不做校验

import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** 
 * 身份证号码校验,正则表达式校验、日期格式校验、18位身份证末尾校验码校验
 */
public class CheckResidentIdentityCard1 {
	/**
	 * 18位身份证中最后一位校验码
	 */
    private final static char[] VERIFY_CODE = {'1','0','X','9','8','7','6','5','4','3','2'}; 
	/**
	 * 18位身份证中,各个数字的生成校验码时的权值
	 */
    private final static int[] VERIFY_CODE_WEIGHT = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    /**	 
     * 方法:身份证号码校验主方法
 	 */
    private static boolean Check(String cardNo) { 
    	//身份证号码不为空
	    if(null == cardNo)
	    	return false;  
	    //去除开头及末尾的多余空格
	    cardNo = cardNo.trim();
        if (15 == cardNo.length())//15位旧身份证号码
        {
        	if(!regex15Check(cardNo))//正则表达式校验
        		return false;  
        	//获取日期
        	String date="19"+cardNo.substring(6,12);
        	if(!isValidDate(date))//日期格式校验
        		return false; 
        	return true;   
        }
        else if(18 == cardNo.length())//18位旧身份证号码
        {
        	if(!regex18Check(cardNo))//正则表达式校验
        		return false;  
        	//获取日期
        	String date=cardNo.substring(6,14);
        	if(!isValidDate(date))//日期格式校验
        		return false; 
        	if(!CheckCode(cardNo))//身份证末尾校验码校验
        		return false; 
            return true;   
        }
        else
        	return false;  
    }
	/**
	 * 方法:判断字符串是否为日期格式,校验平年闰年2月29日28日,校验大月31日小月30日
	 */
	public static boolean isValidDate(String inDate){
		int year = Integer.parseInt(inDate.substring(0,4));//年份
		int month = Integer.parseInt(inDate.substring(4,6));//月份
		int day = Integer.parseInt(inDate.substring(6,8));//天数
		if (month < 1 || month > 12) {//校验月份格式
			return false;
		}
		if (day < 1 || day > 31) {//校验天数格式
			return false;
		}
		if ((month == 4 || month == 6 || month == 9 || month == 11) &&(day == 31)) {//是否不符合4月、6月、9月、11月最大天数30日
			return false;
		}
		boolean leap=false;
		if (month == 2) {//闰年2月为29天,平年2月为28天
			leap = (year % 4 == 0 &&(year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day == 29 && !leap)) {
				return false;
			}
		}
		return true;
	}
	/*      
	 * 		方法:利用正则表达式判断15位旧居民身份证是否符合规范
     * 	    "\\d{8}"                  1~6位分别代表省市县,这里不取字典表校验,只校验是否数字。
     * 								  7~8位代表年份后两位数字
     * 		"(0[1-9]|1[012])"          9~10位代表月份,01~12月
     * 		"(0[1-9]|[12]\\d|3[01])"  11~12位代表日期,1~31日
     * 		"\\d{3}"                  13~15位为三位顺序号
     */
    private static boolean regex15Check(String cardNo) { 

    		Pattern pattern = Pattern.compile("^(\\d{8}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3})$");   
    		Matcher m = pattern.matcher(cardNo); 
    		return (m.matches())?true:false;
	}
    /*		
     * 		方法:利用正则表达式判断15位旧居民身份证是否符合规范
     * 	    "\\d{6}"                  1~6位分别代表省市县,这里不取字典表校验,只校验是否数字。
     * 		"(18|19|20)\\d{2}"        7~10位代表年份,前两位18、19、20即19世纪、20世纪、21世纪,后两位数字。
     * 		中国寿星之首:阿丽米罕·色依提,女,1886年6月25日出生于新疆疏勒县,现年134岁,身份证起始日期在19世纪
     * 		"(0[1-9]|1[012])"          11~12位代表月份,01~12月
     * 		"(0[1-9]|[12]\\d|3[01])"  13~14位代表日期,1~31日
     * 		"\\d{3}"                  15~17位为三位顺序号
     * 		"(\\d|X|x)"               18位为校验位数字,允许字母x和X
     */
    private static boolean regex18Check(String cardNo) { 

    		Pattern pattern = Pattern.compile("^(\\d{6}(18|19|20)\\d{2}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|X|x))$");   
    		Matcher m = pattern.matcher(cardNo); 
    		return (m.matches())?true:false;
	}
    /**
     * 身份证号的第18位校验正确
     */
    public static boolean CheckCode(String cardNo){
        return (calculateVerifyCode(cardNo) == cardNo.charAt(18 - 1));
    }
    /**
     * 校验码(第十八位数)
     * 十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0...16 ,先对前17位数字的权求和;
     * Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2; 
     * 计算模 Y = mod(S, 11)< 通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2
     */
    private static char calculateVerifyCode(CharSequence cardNumber){
        int sum = 0;
        for (int i = 0; i < 18 - 1; i++){
            char ch = cardNumber.charAt(i);
            sum += ((int) (ch - '0')) * VERIFY_CODE_WEIGHT[i];
        }
        return VERIFY_CODE[sum % 11];
    }
	public static void main(String[] args) throws ParseException {
		//数据测试
		String str0="00000018000101123X";
		System.out.println(str0+":"+Check(str0));
		str0="00000x190001011237";
		System.out.println(str0+":"+Check(str0));
		str0="000000210001011234";
		System.out.println(str0+":"+Check(str0));
		str0="000000191301011235";
		System.out.println(str0+":"+Check(str0));
		str0="00000019x301011238";
		System.out.println(str0+":"+Check(str0));
		str0="000000190013011236";
		System.out.println(str0+":"+Check(str0));
		str0="000000190012131236";
		System.out.println(str0+":"+Check(str0));
		str0="000000190012211236";
		System.out.println(str0+":"+Check(str0));
		str0="00000019001221x23x";
		System.out.println(str0+":"+Check(str0));
		str0="000000190001011239";
		System.out.println(str0+":"+Check(str0));
		str0="000000190001011239";	
		System.out.println(str0+":"+Check(str0));
		str0="000000000101123";		
		System.out.println(str0+":"+Check(str0));
		str0="00000000010112x";		
		System.out.println(str0+":"+Check(str0));
		str0="000000000132123";		
		System.out.println(str0+":"+Check(str0));
		str0="000000001322123";		
		System.out.println(str0+":"+Check(str0));
		str0="";		
		System.out.println(str0+":"+Check(str0));
		str0=null;		
		System.out.println(str0+":"+Check(str0));
		str0="123";		
		System.out.println(str0+":"+Check(str0));
		str0="0000001900010112309";		
		System.out.println(str0+":"+Check(str0));	
	}
}

执行main()方法结果:

00000018000101123X:true
00000x190001011237:false
000000210001011234:false
000000191301011235:true
00000019x301011238:false
000000190013011236:false
000000190012131236:true
000000190012211236:true
00000019001221x23x:false
000000190001011239:true
000000190001011239:true
000000000101123:true
00000000010112x:false
000000000132123:false
000000001322123:false
:false
null:false
123:false
0000001900010112309:false

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值