자바 주민등록번호 검사 도구 클래스 상세 설명

본 논문 의 사례 는 자바 주민등록번호 검사 도구 류 의 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;

public class IdcardValidator1 {
 

  public static void main(String[] args) throws Exception { 
     
    String idcard15 = "142431199001145";//15  
    String idcard18 = "121212121212121212";//18  
    IdcardValidator1 iv = new IdcardValidator1(); 
    System.out.println(iv.isValidatedAllIdcard(idcard15)); 
    System.out.println(iv.isValidatedAllIdcard(idcard18)); 
    IdcardInfoExtractor ie = new IdcardInfoExtractor(idcard18); 
    System.out.println(ie.toString()); 
  } 
 
 
 /** 
   *  ,      : { 11:"  ",12:"  ",13:"  ",14:"  ",15:"   ", 
   * 21:"  ",22:"  ",23:"   ",31:"  ",32:"  ", 
   * 33:"  ",34:"  ",35:"  ",36:"  ",37:"  ",41:"  ", 
   * 42:"  ",43:"  ",44:"  ",45:"  ",46:"  ",50:"  ", 
   * 51:"  ",52:"  ",53:"  ",54:"  ",61:"  ",62:"  ", 
   * 63:"  ",64:"  ",65:"  ",71:"  ",81:"  ",82:"  ",91:"  "} 
   */ 
  protected String codeAndCity[][] = { { "11", "  " }, { "12", "  " }, 
      { "13", "  " }, { "14", "  " }, { "15", "   " }, { "21", "  " }, 
      { "22", "  " }, { "23", "   " }, { "31", "  " }, { "32", "  " }, 
      { "33", "  " }, { "34", "  " }, { "35", "  " }, { "36", "  " }, 
      { "37", "  " }, { "41", "  " }, { "42", "  " }, { "43", "  " }, 
      { "44", "  " }, { "45", "  " }, { "46", "  " }, { "50", "  " }, 
      { "51", "  " }, { "52", "  " }, { "53", "  " }, { "54", "  " }, 
      { "61", "  " }, { "62", "  " }, { "63", "  " }, { "64", "  " }, 
      { "65", "  " }, { "71", "  " }, { "81", "  " }, { "82", "  " }, 
      { "91", "  " } }; 
 
  private String cityCode[] = { "11", "12", "13", "14", "15", "21", "22", 
      "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", 
      "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", 
      "64", "65", "71", "81", "82", "91" }; 
 
  //        
  private int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; 
 
  //  18     
  private String verifyCode[] = { "1", "0", "X", "9", "8", "7", "6", "5", 
      "4", "3", "2" }; 
 
  /** 
   *              
   * 
   * @param idcard 
   * @return 
   */ 
  public boolean isValidatedAllIdcard(String idcard) { 
    if (idcard.length() == 15) { 
      idcard = this.convertIdcarBy15bit(idcard); 
    } 
    return this.isValidate18Idcard(idcard); 
  } 
 
  /** 
   * <p> 
   *   18         
   * </p> 
   *   〖           GB11643-1999〗            ,            ,                   。 
   *            :       ,         ,               。 
   * <p> 
   *    :                  ,   、  、             ,           ,        。 
   * </p> 
   * <p> 
   * 1. 1、2     :       ; 2. 3、4     :       ; 3. 5、6     :       ; 
   * 4. 7~14     :   、 、 ; 5. 15、16     :          ; 
   * 6. 17       :      ,      ; 
   * 7. 18       :          ,            ,           。      0~9   ,    x  。 
   * </p> 
   * <p> 
   *       (   )      : 1.         17           。               :7 9 10 5 8 4 
   * 2 1 6 3 7 9 10 5 8 4 2 
   * </p> 
   * <p> 
   * 2.  17             。 
   * </p> 
   * <p> 
   * 3.       11,      ? 
   * </p> 
   * 4.      0 1 2 3 4 5 6 7 8 9 10 11   。                 1 0 X 9 8 7 6 5 4 3 
   * 2。 
   * <p> 
   * 5.           2,        18           Ⅹ。     10,            2。 
   * </p> 
   * 
   * @param idcard 
   * @return 
   */ 
  public boolean isValidate18Idcard(String idcard) { 
    //  18    
    if (idcard.length() != 18) { 
      return false; 
    } 
    //    17  
    String idcard17 = idcard.substring(0, 17); 
    //    18  
    String idcard18Code = idcard.substring(17, 18); 
    char c[] = null; 
    String checkCode = ""; 
    //        
    if (isDigital(idcard17)) { 
      c = idcard17.toCharArray(); 
    } else { 
      return false; 
    } 
 
    if (null != c) { 
      int bit[] = new int[idcard17.length()]; 
 
      bit = converCharToInt(c); 
 
      int sum17 = 0; 
 
      sum17 = getPowerSum(bit); 
 
      //     11              
      checkCode = getCheckCodeBySum(sum17); 
      if (null == checkCode) { 
        return false; 
      } 
      //       18            ,       
      if (!idcard18Code.equalsIgnoreCase(checkCode)) { 
        return false; 
      } 
    } 
    return true; 
  } 
 
  /** 
   *   15        ,        ,    15  18     ,      。 
   * 
   * @param idcard 
   * @return 
   */ 
  public boolean isValidate15Idcard(String idcard) { 
    //  15    
    if (idcard.length() != 15) { 
      return false; 
    } 
 
    //         
    if (isDigital(idcard)) { 
      String provinceid = idcard.substring(0, 2); 
      String birthday = idcard.substring(6, 12); 
      int year = Integer.parseInt(idcard.substring(6, 8)); 
      int month = Integer.parseInt(idcard.substring(8, 10)); 
      int day = Integer.parseInt(idcard.substring(10, 12)); 
 
      //            
      boolean flag = false; 
      for (String id : cityCode) { 
        if (id.equals(provinceid)) { 
          flag = true; 
          break; 
        } 
      } 
      if (!flag) { 
        return false; 
      } 
      //                    
      Date birthdate = null; 
      try { 
        birthdate = new SimpleDateFormat("yyMMdd").parse(birthday); 
      } catch (ParseException e) { 
        e.printStackTrace(); 
      } 
      if (birthdate == null || new Date().before(birthdate)) { 
        return false; 
      } 
 
      //            
      GregorianCalendar curDay = new GregorianCalendar(); 
      int curYear = curDay.get(Calendar.YEAR); 
      int year2bit = Integer.parseInt(String.valueOf(curYear) 
          .substring(2)); 
 
      //            ,  50         ,   
      if ((year < 50 && year > year2bit)) { 
        return false; 
      } 
 
      //            
      if (month < 1 || month > 12) { 
        return false; 
      } 
 
      //            
      boolean mflag = false; 
      curDay.setTime(birthdate); //               curDay 
      switch (month) { 
      case 1: 
      case 3: 
      case 5: 
      case 7: 
      case 8: 
      case 10: 
      case 12: 
        mflag = (day >= 1 && day <= 31); 
        break; 
      case 2: //    2     28 ,   2  29 。 
        if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) { 
          mflag = (day >= 1 && day <= 29); 
        } else { 
          mflag = (day >= 1 && day <= 28); 
        } 
        break; 
      case 4: 
      case 6: 
      case 9: 
      case 11: 
        mflag = (day >= 1 && day <= 30); 
        break; 
      } 
      if (!mflag) { 
        return false; 
      } 
    } else { 
      return false; 
    } 
    return true; 
  } 
 
  /** 
   *  15       18     
   * 
   * @param idcard 
   * @return 
   */ 
  public String convertIdcarBy15bit(String idcard) { 
    String idcard17 = null; 
    //  15     
    if (idcard.length() != 15) { 
      return null; 
    } 
 
    if (isDigital(idcard)) { 
      //         
      String birthday = idcard.substring(6, 12); 
      Date birthdate = null; 
      try { 
        birthdate = new SimpleDateFormat("yyMMdd").parse(birthday); 
      } catch (ParseException e) { 
        e.printStackTrace(); 
      } 
      Calendar cday = Calendar.getInstance(); 
      cday.setTime(birthdate); 
      String year = String.valueOf(cday.get(Calendar.YEAR)); 
 
      idcard17 = idcard.substring(0, 6) + year + idcard.substring(8); 
 
      char c[] = idcard17.toCharArray(); 
      String checkCode = ""; 
 
      if (null != c) { 
        int bit[] = new int[idcard17.length()]; 
 
        //             
        bit = converCharToInt(c); 
        int sum17 = 0; 
        sum17 = getPowerSum(bit); 
 
        //      11            
        checkCode = getCheckCodeBySum(sum17); 
        //         
        if (null == checkCode) { 
          return null; 
        } 
 
        //   17   18       
        idcard17 += checkCode; 
      } 
    } else { //         
      return null; 
    } 
    return idcard17; 
  } 
 
  /** 
   * 15  18                 
   * 
   * @param idcard 
   * @return 
   */ 
  public boolean isIdcard(String idcard) { 
    return idcard == null || "".equals(idcard) ? false : Pattern.matches( 
        "(^\\d{15}$)|(\\d{17}(?:\\d|x|X)$)", idcard); 
  } 
 
  /** 
   * 15                 
   * 
   * @param idcard 
   * @return 
   */ 
  public boolean is15Idcard(String idcard) { 
    return idcard == null || "".equals(idcard) ? false : Pattern.matches( 
        "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$", 
        idcard); 
  } 
 
  /** 
   * 18                 
   * 
   * @param idcard 
   * @return 
   */ 
  public boolean is18Idcard(String idcard) { 
    return Pattern 
        .matches( 
            "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$", 
            idcard); 
  } 
 
  /** 
   *      
   * 
   * @param str 
   * @return 
   */ 
  public boolean isDigital(String str) { 
    return str == null || "".equals(str) ? false : str.matches("^[0-9]*$"); 
  } 
 
  /** 
   *                     ,      
   * 
   * @param bit 
   * @return 
   */ 
  public int getPowerSum(int[] bit) { 
 
    int sum = 0; 
 
    if (power.length != bit.length) { 
      return sum; 
    } 
 
    for (int i = 0; i < bit.length; i++) { 
      for (int j = 0; j < power.length; j++) { 
        if (i == j) { 
          sum = sum + bit[i] * power[j]; 
        } 
      } 
    } 
    return sum; 
  } 
 
  /** 
   *     11              
   * 
   * @param checkCode 
   * @param sum17 
   * @return     
   */ 
  public String getCheckCodeBySum(int sum17) { 
    String checkCode = null; 
    switch (sum17 % 11) { 
    case 10: 
      checkCode = "2"; 
      break; 
    case 9: 
      checkCode = "3"; 
      break; 
    case 8: 
      checkCode = "4"; 
      break; 
    case 7: 
      checkCode = "5"; 
      break; 
    case 6: 
      checkCode = "6"; 
      break; 
    case 5: 
      checkCode = "7"; 
      break; 
    case 4: 
      checkCode = "8"; 
      break; 
    case 3: 
      checkCode = "9"; 
      break; 
    case 2: 
      checkCode = "x"; 
      break; 
    case 1: 
      checkCode = "0"; 
      break; 
    case 0: 
      checkCode = "1"; 
      break; 
    } 
    return checkCode; 
  } 
 
  /** 
   *             
   * 
   * @param c 
   * @return 
   * @throws NumberFormatException 
   */ 
  public int[] converCharToInt(char[] c) throws NumberFormatException { 
    int[] a = new int[c.length]; 
    int k = 0; 
    for (char temp : c) { 
      a[k++] = Integer.parseInt(String.valueOf(temp)); 
    } 
    return a; 
  } 
 
}

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class IdcardInfoExtractor {
 
 
 public static void main(String[] args) { 
    String idcard = "342221199406205511"; 
    IdcardInfoExtractor ie = new IdcardInfoExtractor(idcard); 
    System.out.println(ie.toString()); 
  } 
 
 
 //    
  private String province; 
  //    
  private String city; 
  //    
  private String region; 
  //    
  private int year; 
  //    
  private int month; 
  //    
  private int day; 
  //     1  2 
  private int gender; 
  //      
  private Date birthday; 
 
  private Map<String, String> cityCodeMap = new HashMap<String, String>() { 
    { 
      this.put("11", "  "); 
      this.put("12", "  "); 
      this.put("13", "  "); 
      this.put("14", "  "); 
      this.put("15", "   "); 
      this.put("21", "  "); 
      this.put("22", "  "); 
      this.put("23", "   "); 
      this.put("31", "  "); 
      this.put("32", "  "); 
      this.put("33", "  "); 
      this.put("34", "  "); 
      this.put("35", "  "); 
      this.put("36", "  "); 
      this.put("37", "  "); 
      this.put("41", "  "); 
      this.put("42", "  "); 
      this.put("43", "  "); 
      this.put("44", "  "); 
      this.put("45", "  "); 
      this.put("46", "  "); 
      this.put("50", "  "); 
      this.put("51", "  "); 
      this.put("52", "  "); 
      this.put("53", "  "); 
      this.put("54", "  "); 
      this.put("61", "  "); 
      this.put("62", "  "); 
      this.put("63", "  "); 
      this.put("64", "  "); 
      this.put("65", "  "); 
      this.put("71", "  "); 
      this.put("81", "  "); 
      this.put("82", "  "); 
      this.put("91", "  "); 
    } 
  }; 
 
  private IdcardValidator1 validator = null; 
 
  /** 
   *                 
   */ 
  public IdcardInfoExtractor(String idcard) { 
    try { 
      validator = new IdcardValidator1(); 
      if (validator.isValidatedAllIdcard(idcard)) { 
        if (idcard.length() == 15) { 
          idcard = validator.convertIdcarBy15bit(idcard); 
        } 
        //      
        String provinceId = idcard.substring(0, 2); 
        Set<String> key = this.cityCodeMap.keySet(); 
        for (String id : key) { 
          if (id.equals(provinceId)) { 
            this.province = this.cityCodeMap.get(id); 
            break; 
          } 
        } 
 
        //      
        String id17 = idcard.substring(16, 17); 
        if (Integer.parseInt(id17) % 2 != 0) { 
          this.gender = 1; // 
        } else { 
          this.gender = 2; // 
        } 
 
        //        
        String birthday = idcard.substring(6, 14); 
        Date birthdate = new SimpleDateFormat("yyyyMMdd") 
            .parse(birthday); 
        this.birthday = birthdate; 
        GregorianCalendar currentDay = new GregorianCalendar(); 
        currentDay.setTime(birthdate); 
        this.year = currentDay.get(Calendar.YEAR); 
        this.month = currentDay.get(Calendar.MONTH) + 1; 
        this.day = currentDay.get(Calendar.DAY_OF_MONTH); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
 
  /** 
   * @return the province 
   */ 
  public String getProvince() { 
    return province; 
  } 
 
  /** 
   * @return the city 
   */ 
  public String getCity() { 
    return city; 
  } 
 
  /** 
   * @return the region 
   */ 
  public String getRegion() { 
    return region; 
  } 
 
  /** 
   * @return the year 
   */ 
  public int getYear() { 
    return year; 
  } 
 
  /** 
   * @return the month 
   */ 
  public int getMonth() { 
    return month; 
  } 
 
  /** 
   * @return the day 
   */ 
  public int getDay() { 
    return day; 
  } 
 
  /** 
   * @return the gender 
   */ 
  public int getGender() { 
    return gender; 
  } 
 
  /** 
   * @return the birthday 
   */ 
  public Date getBirthday() { 
    return birthday; 
  } 
 
  @Override 
  public String toString() { 
    return "  :" + this.province + ",  :" + this.gender + ",    :" 
        + this.birthday; 
  } 
}

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기