熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

中華人民共和國公民身份證號碼校驗程序

2022-06-13   來源: Java核心技術 

  /**

  * roontenpracticetoolsidentitycard\IdentityCardjava

  *

  * Copyright (c) patulous function toolkit for java individual

  * All rights reserved

  *

  * This toolkit all my items one is the last edition All the tools in the toolkit are defining

  * some handy discreteness for me So I can doing items easily I hope you unfeignedly

  * for use it

  */

  package roontenpracticetoolsidentitycard;

  import javautilCalendar;

  /**

  * <div>

  * The <code>IdentityCard</code> class is implements IIdentityCard

  * interfaceIn this interface therere some about checking an identity

  * card need to using methods For example the year and the month and the day

  * part this identity card must be right

  * </div>

  *

  * @author Roon Ten

  * @version

  */

  public class IdentityCard {

  private char[] chrs;

  private final String CHECK_STRING = ([]{})[Xx];

  /**

  * Create an object of this class by an scheduled identity card

  * @param obj

  */

  public IdentityCard(Object obj) {

  chrs = getChrs(obj);

  }

  /**

  * Get a team for character by this identity card

  * @param obj

  * @return

  */

  private char[] getChrs(Object obj) {

  return StringvalueOf(obj)toCharArray();

  }

  /**

  * If this identity card is legal one return true

  */

  public boolean isLegalityFormat() {

  return StringvalueOf(thischrs)matches(CHECK_STRING);

  }

  /**

  * If the year this identity card is legal one return true

  */

  public boolean isLegalityYear() {

  boolean isPass = false;

  if (chrs != null && chrslength >= ) {

  int year = IntegerparseInt(chrs[] + + chrs[] + + chrs[]

  + + chrs[]);

  int current = CalendargetInstance()get(CalendarYEAR);

  isPass = year >= current && year <= current;

  }

  return isPass;

  }

  /**

  * If the month this identity card is legal one return true

  */

  public boolean isLegalityMonth() {

  boolean isPass = false;

  if (chrs != null && chrslength >= ) {

  int month = IntegerparseInt(chrs[] + + chrs[]);

  isPass = month > && month <= ;

  }

  return isPass;

  }

  /**

  * If the day this identity card is legal one return true

  */

  public boolean isLegalityDay() {

  boolean isPass = false;

  if (chrs != null && chrslength >= ) {

  int yy = IntegerparseInt(chrs[] + + chrs[] + + chrs[]

  + + chrs[]);

  int month = IntegerparseInt(chrs[] + + chrs[]);

  int day = IntegerparseInt(chrs[] + + chrs[]);

  Year year = new Year(yy);

  isPass = yeargetMaxDay(month) >= day;

  }

  return isPass;

  }

  /**

  * If the last character this identity card is legal one return true

  */

  public boolean isLegalityEnd() {

  boolean isPass = false;

  if (chrs != null && chrslength >= ) {

  int num = (IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) *

  + IntegerparseInt(chrs[] + ) * + Integer

  parseInt(chrs[] + ) * ) % ;

  switch (num) {

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = X == chrs[] || x == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  case :

  isPass = == chrs[];

  break;

  }

  }

  return isPass;

  }

  /**

  * Get the length this identity card

  */

  public int getLength() {

  return thischrslength;

  }

  /**

  * If it is a legal one

  */

  public boolean isEffective() {

  boolean isValid = false;

  isValid = isLegalityFormat();

  if (isValid) {

  isValid = isLegalityYear();

  if (isValid) {

  isValid = isLegalityMonth();

  if (isValid) {

  isValid = isLegalityDay();

  if (isValid) {

  isValid = isLegalityEnd();

  }

  }

  }

  }

  return isValid;

  }

  }

  /**

  * The <code>Year</code> class use to get a month day of a year

  * @author TenRoon

  * @version

  */

  class Year {

  /**

  * Current year

  */

  private int Year;

  /**

  * January days

  */

  public static final int JAN_DAY = ;

  /**

  * February days of a year

  * @param year

  * @return

  */

  public static final int FEB_DAY(int year) {

  int feb_Day = ;

  if ((year % == && year % != ) || year % == ) {

  feb_Day = ;

  }

  return feb_Day;

  }

  /**

  * March days

  */

  public static final int MAR_DAY = ;

  /**

  * April days

  */

  public static final int APR_DAY = ;

  /**

  * May days

  */

  public static final int MAY_DAY = ;

  /**

  * June days

  */

  public static final int JUN_DAY = ;

  /**

  * July days

  */

  public static final int JUL_DAY = ;

  /**

  * August days

  */

  public static final int AUG_DAY = ;

  /**

  * September days

  */

  public static final int SEP_DAY = ;

  /**

  * Octomber days

  */

  public static final int OCT_DAY = ;

  /**

  * November days

  */

  public static final int NOV_DAY = ;

  /**

  * December days

  */

  public static final int DEC_DAY = ;

  /**

  * Create a object of a current year

  * @param Year

  */

  public Year(int Year) {

  thisYear = Year;

  }

  /**

  * Get the max day a month

  * @param month

  * @return

  */

  public int getMaxDay(int month) {

  int maxDay = ;

  switch (month) {

  case :

  maxDay = JAN_DAY;

  break;

  case :

  maxDay = FEB_DAY(thisYear);

  break;

  case :

  maxDay = MAR_DAY;

  break;

  case :

  maxDay = APR_DAY;

  break;

  case :

  maxDay = MAY_DAY;

  break;

  case :

  maxDay = JUN_DAY;

  break;

  case :

  maxDay = JUL_DAY;

  break;

  case :

  maxDay = AUG_DAY;

  break;

  case :

  maxDay = SEP_DAY;

  break;

  case :

  maxDay = OCT_DAY;

  break;

  case :

  maxDay = NOV_DAY;

  break;

  case :

  maxDay = DEC_DAY;

  break;

  }

  return maxDay;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25785.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.