【转】身份证号码校验与信息提取 - Java 代码

转载地址:http://www.w3china.org/blog/more.asp?name=lhwork&id=19148

import java.util.regex.*;
 
/**
 *<p>Title:验证18/15位中国公民身份证号码,并分析有效的数据</p>
 *<p>Description:</p>
 *<p>Copyright:Copyright(c)2004</p>
 *<p>Company:Labile</p>
 *@authornotattributable
 *@version1.0
 */
publicclass Verifier {
 
 privatestaticfinal String verify18PatternStr = "(\\d{6})" + "(\\d{8})" + "(\\d{3})" + "([\\d[xX]]{1})";
 privatestaticfinal Pattern verify18Pattern = Pattern.compile(verify18PatternStr);
 privatestaticfinal String verify15PatternStr = "(\\d{6})" + "(\\d{6})" + "(\\d{3})";
 privatestaticfinal Pattern verify15Pattern = Pattern.compile(verify15PatternStr);
 
 
 /**
   *从第18到第1位的权
   */
 privatestaticfinalintverify18Rights[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
 /**
   *检验码校对表
   */
 privatestaticfinal String verify18CheckDigit[] = {"1","0","X","9","8","7","6","5","4","3","2"};
 
 
 /**
   *地区码
   */
 private String areaNum;
 /**
   *出生时间
   */
 private String birthdayNum;
 /**
   *顺序号
   */
 private String seqNum;
 /**
   *校验码
   */
 private String check_digit;
 
 privateintlength;
 
 publicboolean isMan() {
    int num = Integer.parseInt(seqNum);
    if(num%2==1) {
      returntrue;
    }
    else
      returnfalse;
 }
 
 /**
   *得到生日的日期,格式:yyyy-mm-dd
   *@returnString
   */
 public String getBirthday() {
    String FullBirthdayNum = null;
    if(length==15)//在生日号码前加“19”
      FullBirthdayNum = "19" + birthdayNum;
    else
      FullBirthdayNum = birthdayNum;
 
    String year = FullBirthdayNum.substring(0, 4);
    String month = FullBirthdayNum.substring(4, 6);
    String date = FullBirthdayNum.substring(6, 8);
    return year + "-" + month + "-" + date;
 }
 
 public Verifier(String IDCardNO) throws LengthIllegalException, CheckDigitInvalidException, NumberInvalidException {
    if(IDCardNO == null)
      thrownew NumberInvalidException();
    length = IDCardNO.length();
    switch(length) {
      case 15:
        Verifier15(IDCardNO);
        break;
      case 18:
        Verifier18(IDCardNO);
        break;
      default:
        thrownew LengthIllegalException();
    }
 
 }
 
 privatevoid Verifier15(String IDCardNO) throws NumberInvalidException {
 
    Matcher m = verify15Pattern.matcher(IDCardNO);
    if(m.matches()==false) {
      thrownew NumberInvalidException();
    }
    areaNum = m.group(1);
    birthdayNum = m.group(2);
    seqNum = m.group(3);
 }
 
 privatevoid Verifier18(String IDCardNO) throws CheckDigitInvalidException, NumberInvalidException {
 
    Matcher m = verify18Pattern.matcher(IDCardNO);
    if(m.matches()==false) {
      thrownew NumberInvalidException();
    }
    areaNum = m.group(1);
    birthdayNum = m.group(2);
    seqNum = m.group(3);
    check_digit = m.group(4);
 
 
    //预期的校验位:
    String expect_check_digit = getCheck_digit18(IDCardNO);
 
    //如果校验位无效
    if(expect_check_digit.equalsIgnoreCase(check_digit)==false)
      thrownew CheckDigitInvalidException();
 }
 
 /**
   *18/17位身份证号算出校验位
   *@paramIDCardNOString
   *@returnString
   */
 publicstatic String getCheck_digit18(String IDCardNO) {
    //权总值
    int sum = 0;
    for(int i=0; i<=16; i++) {
      int num = Integer.parseInt( IDCardNO.substring(i,i + 1) );
      int right = verify18Rights[i];
      sum = sum + num*right;
    }
    //对权总值取模
    int y = sum % 11;
    returnverify18CheckDigit[y];
 }
 
 publicstatic String IDCardNO15To18(String IDCardNO15) throws
      NumberInvalidException {
 
    Matcher m = verify15Pattern.matcher(IDCardNO15);
    if(m.matches()==false) {
      thrownew NumberInvalidException();
    }
 
    String NO17 = IDCardNO15.substring(0,6) + "19" + IDCardNO15.substring(6,15);
    return NO17 + getCheck_digit18(NO17);
 }
 
 public String getAreaNum() {
    returnareaNum;
 }
 
 public String getBirthdayNum() {
    returnbirthdayNum;
 }
 
 /**
   *返回校验位,只有18位身份证可用,15位时返回null
   *@returnString
   */
 public String getCheck_digitNum18() {
    returncheck_digit;
 }
 publicint getLength() {
    returnlength;
 }
 public String getSeqNum() {
    returnseqNum;
 }
 
}
 
import java.util.regex.*;
 
/**
 *<p>Title:验证18/15位中国公民身份证号码,并分析有效的数据</p>
 *<p>Description:</p>
 *<p>Copyright:Copyright(c)2004</p>
 *<p>Company:Labile</p>
 *@authornotattributable
 *@version1.0
 */
publicclass Verifier {
 
 privatestaticfinal String verify18PatternStr = "(\\d{6})" + "(\\d{8})" + "(\\d{3})" + "([\\d[xX]]{1})";
 privatestaticfinal Pattern verify18Pattern = Pattern.compile(verify18PatternStr);
 privatestaticfinal String verify15PatternStr = "(\\d{6})" + "(\\d{6})" + "(\\d{3})";
 privatestaticfinal Pattern verify15Pattern = Pattern.compile(verify15PatternStr);
 
 
 /**
   *从第18到第1位的权
   */
 privatestaticfinalintverify18Rights[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
 /**
   *检验码校对表
   */
 privatestaticfinal String verify18CheckDigit[] = {"1","0","X","9","8","7","6","5","4","3","2"};
 
 
 /**
   *地区码
   */
 private String areaNum;
 /**
   *出生时间
   */
 private String birthdayNum;
 /**
   *顺序号
   */
 private String seqNum;
 /**
   *校验码
   */
 private String check_digit;
 
 privateintlength;
 
 publicboolean isMan() {
    int num = Integer.parseInt(seqNum);
    if(num%2==1) {
      returntrue;
    }
    else
      returnfalse;
 }
 
 /**
   *得到生日的日期,格式:yyyy-mm-dd
   *@returnString
   */
 public String getBirthday() {
    String FullBirthdayNum = null;
    if(length==15)//在生日号码前加“19”
      FullBirthdayNum = "19" + birthdayNum;
    else
      FullBirthdayNum = birthdayNum;
 
    String year = FullBirthdayNum.substring(0, 4);
    String month = FullBirthdayNum.substring(4, 6);
    String date = FullBirthdayNum.substring(6, 8);
    return year + "-" + month + "-" + date;
 }
 
 public Verifier(String IDCardNO) throws LengthIllegalException, CheckDigitInvalidException, NumberInvalidException {
    if(IDCardNO == null)
      thrownew NumberInvalidException();
    length = IDCardNO.length();
    switch(length) {
      case 15:
        Verifier15(IDCardNO);
        break;
      case 18:
        Verifier18(IDCardNO);
        break;
      default:
        thrownew LengthIllegalException();
    }
 
 }
 
 privatevoid Verifier15(String IDCardNO) throws NumberInvalidException {
 
    Matcher m = verify15Pattern.matcher(IDCardNO);
    if(m.matches()==false) {
      thrownew NumberInvalidException();
    }
    areaNum = m.group(1);
    birthdayNum = m.group(2);
    seqNum = m.group(3);
 }
 
 privatevoid Verifier18(String IDCardNO) throws CheckDigitInvalidException, NumberInvalidException {
 
    Matcher m = verify18Pattern.matcher(IDCardNO);
    if(m.matches()==false) {
      thrownew NumberInvalidException();
    }
    areaNum = m.group(1);
    birthdayNum = m.group(2);
    seqNum = m.group(3);
    check_digit = m.group(4);
 
 
    //预期的校验位:
    String expect_check_digit = getCheck_digit18(IDCardNO);
 
    //如果校验位无效
    if(expect_check_digit.equalsIgnoreCase(check_digit)==false)
      thrownew CheckDigitInvalidException();
 }
 
 /**
   *18/17位身份证号算出校验位
   *@paramIDCardNOString
   *@returnString
   */
 publicstatic String getCheck_digit18(String IDCardNO) {
    //权总值
    int sum = 0;
    for(int i=0; i<=16; i++) {
      int num = Integer.parseInt( IDCardNO.substring(i,i + 1) );
      int right = verify18Rights[i];
      sum = sum + num*right;
    }
    //对权总值取模
    int y = sum % 11;
    returnverify18CheckDigit[y];
 }
 
 publicstatic String IDCardNO15To18(String IDCardNO15) throws
      NumberInvalidException {
 
    Matcher m = verify15Pattern.matcher(IDCardNO15);
    if(m.matches()==false) {
      thrownew NumberInvalidException();
    }
 
    String NO17 = IDCardNO15.substring(0,6) + "19" + IDCardNO15.substring(6,15);
    return NO17 + getCheck_digit18(NO17);
 }
 
 public String getAreaNum() {
    returnareaNum;
 }
 
 public String getBirthdayNum() {
    returnbirthdayNum;
 }
 
 /**
   *返回校验位,只有18位身份证可用,15位时返回null
   *@returnString
   */
 public String getCheck_digitNum18() {
    returncheck_digit;
 }
 publicint getLength() {
    returnlength;
 }
 public String getSeqNum() {
    returnseqNum;
 }
 
}
 

【转】身份证号码校验与信息提取 - Java 代码

上一篇:WIndows系统下宽窄字符转换(待更新)


下一篇:第一个windows桌面应用程序