Java:15位或18位居民⾝份证号码通⽤校验(正则表达式、
⽇期格式、末尾校验码)
⾝份证号码校验,正则表达式校验、⽇期格式校验、18位⾝份证末尾校验码校验
前六位省市县号码变更频繁,这⾥就不做校验
ParseException;
import Matcher;
import Pattern;
/**
* ⾝份证号码校验,正则表达式校验、⽇期格式校验、18位⾝份证末尾校验码校验
*/
public class CheckResidentIdentityCard1 {
/
**
* 18位⾝份证中最后⼀位校验码
*/
private final static char[] VERIFY_CODE = {'1','0','X','9','8','7','6','5','4','3','2'};
/**
* 18位⾝份证中,各个数字的⽣成校验码时的权值
*/
时间正则表达式javaprivate 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 = im();
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 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 = Patternpile("^(\\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 = Patternpile("^(\\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);
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论