SoFunction
Updated on 2025-03-11

Android ID number validity verification tool case

I don't remember where I found it. I modified some code to fix the problem of abnormal time zone and time format when used on the Android platform.

package ;
import ;
import ;
import ;
import ;

/**
  * Created by Arjun on 2017/4/25.
  * Identity card validity verification
  */

public class IdentityUtils {
  // Bit weight array  private static byte[] Wi = new byte[17];
  // Number of characters before the ID card  private static final byte fPart = 6;
  // Find the key value of the ID card algorithm  private static final byte fMod = 11;
  // Length of old ID card  private static final byte oldIDLen = 15;
  // New ID card length  private static final byte newIDLen = 18;
  // New ID card year logo  private static final String yearFlag = "19";
  // Verification code string  private static final String CheckCode = "10X98765432";
  // The smallest administrative division code  private static final int minCode = 150000;
  // The largest administrative division code  private static final int maxCode = 700000;
// Old ID number//  private String oldIDCard="";
// New ID number//  private String newIDCard="";
// Region and code
  //private String Area[][2] =
  private static void setWiBuffer() {
    for (int i = 0; i < ; i++) {
      int k = (int) (2, ( - i));
      Wi[i] = (byte) (k % fMod);
    }
  }

  //Get the last digit of the new ID card: the verification digit  private static String getCheckFlag(String idCard) {
    int sum = 0;
    //Perform weighted sum    for (int i = 0; i < 17; i++) {
      sum += ((i, i + 1)) * Wi[i];
    }
    //Take the modulus operation and get the modulus value    byte iCode = (byte) (sum % fMod);
    return (iCode, iCode + 1);
  }

  //Judge the legality of the length of the string  private static boolean checkLength(final String idCard, boolean newIDFlag) {
    boolean right = (() == oldIDLen) || (() == newIDLen);
    newIDFlag = false;
    if (right) {
      newIDFlag = (() == newIDLen);
    }
    return right;
  }

  //Get the time string  private static String getIDDate(final String idCard, boolean newIDFlag) {
    String dateStr = "";
    if (newIDFlag)
      dateStr = (fPart, fPart + 8);
    else
      dateStr = yearFlag + (fPart, fPart + 6);
    return dateStr;
  }

  //Judge the legality of time  private static boolean checkDate(final String dateSource) {
    String dateStr = (0, 4) + "-" + (4, 6) + "-" + (6, 8);
    (dateStr);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    try {
      Date date = (dateStr);
      return (date != null);
    } catch ( e) {
      ();
      return false;
    }
  }

  //Convert the old ID card to a new ID card number  public static String getNewIDCard(final String oldIDCard) {
    //Initialization method    ();
    if (!checkIDCard(oldIDCard)) {
      return oldIDCard;
    }
    String newIDCard = (0, fPart);
    newIDCard += yearFlag;
    newIDCard += (fPart, ());
    String ch = getCheckFlag(newIDCard);
    newIDCard += ch;
    return newIDCard;
  }

  //Convert the new ID card to an old ID card number  public static String getOldIDCard(final String newIDCard) {
    //Initialization method    ();
    if (!checkIDCard(newIDCard)) {
      return newIDCard;
    }
    String oldIDCard = (0, fPart) +
        (fPart + (), () - 1);
    return oldIDCard;
  }

  //Judge the legality of the ID number  public static boolean checkIDCard(final String idCard) {
    //Initialization method    ();

    int length = ();
    boolean isNew;
    if (length == oldIDLen) isNew = false;
    else if (length == newIDLen) isNew = true;
    else return false;

    //String message = "";
    if (!checkLength(idCard, isNew)) {
      //message = "ID length exception";      return false;
    }
    String idDate = getIDDate(idCard, isNew);
    if (!checkDate(idDate)) {
      //message = "ID time exception";      return false;
    }
    if (isNew) {
      String checkFlag = getCheckFlag(idCard);
      String theFlag = (() - 1, ());
      if (!(theFlag)) {
        //message = "New ID card check bit is abnormal";        return false;
      }
    }
    return true;
  }

  //Get a random "pseudo" ID number  public static String getRandomIDCard(final boolean idNewID) {
    //Initialization method    ();
    Random ran = new Random();
    String idCard = getAddressCode(ran) + getRandomDate(ran, idNewID) + getIDOrder(ran);
    if (idNewID) {
      String ch = getCheckFlag(idCard);
      idCard += ch;
    }
    return idCard;
  }

  //Create random region code  private static String getAddressCode(Random ran) {
    if (ran == null) {
      return "";
    } else {
      int addrCode = minCode + (maxCode - minCode);
      return (addrCode);
    }
  }

  //Create a random date of birth  private static String getRandomDate(Random ran, boolean idNewID) {
    // TODO Auto-generated method stub
    if (ran == null) {
      return "";
    }
    int year = 0;
    if (idNewID) {
      year = 1900 + (2017 - 1900);
    } else {
      year = 1 + (99);
    }
    int month = 1 + (12);
    int day = 0;
    if (month == 2) {
      day = 1 + (28);
    } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      day = 1 + (31);
    } else {
      day = 1 + (30);
    }
    NumberFormat nf = ();
    (2);
    (2);
    String dateStr = (year) + (month) + (day);
    return dateStr;
  }

  //Generate random sequence numbers  private static String getIDOrder(Random ran) {
    // TODO Auto-generated method stub
    NumberFormat nf = ();
    (3);
    (3);
    if (ran == null) {
      return "";
    } else {
      int order = 1 + (999);
      return (order);
    }
  }

  public IdentityUtils() {
    setWiBuffer();
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    boolean checkFlag = ("512501197203035172");
    (checkFlag);
  }
}

Supplementary knowledge:Android [ID card verification method] Encapsulated Can be called directly Available

When working on a project, I need to verify the user's ID card. It is difficult to write one separately, so I asked the Baidu Girls for help and tried several methods. This method is more comprehensive and practical.

I also thank the original author for recording this, but now I forgot where to copy it

import ; 
/**
  * Identity card tool class
  */
public class IdCardUtil {
 
  private String idCardNum = null; 
  private static int IS_EMPTY = 1;
  private static int LEN_ERROR = 2;
  private static int CHAR_ERROR = 3;
  private static int DATE_ERROR = 4;
  private static int CHECK_BIT_ERROR = 5; 
  private String[] errMsg = new String[]{"The ID card is completely correct!",
      "Identity card is empty!",
      "The ID card is incorrect!",
      "Identity card has illegal characters!",
      "The date of birth in the ID card is illegal!",
      "Identity card check digit error!"};
 
  private int error = 0;
 
  /**
    * Construct method.
    *
    * @param idCardNum
    */
  public IdCardUtil(String idCardNum) {
    // super();
     = ();
    if (!()) {
       = ("x", "X");
    }
  }
 
  public String getIdCardNum() {
    return idCardNum;
  }
 
  public void setIdCardNum(String idCardNum) {
     = idCardNum;
    if (!()) {
       = ("x", "X");
    }
  }
 
  /**
    * Get detailed error information of the ID card.
    *
    * @return Error message.
    */
  public String getErrMsg() {
    return [];
  }
 
  /**
    * Whether it is empty.
    *
    * @return true: null false: not null;
    */
  public boolean isEmpty() {
    if ( == null)
      return true;
    else
      return ().length() > 0 ? false : true;
  }
 
  /**
    * ID card length.
    *
    * @return
    */
  public int getLength() {
    return () ? 0 : ();
  }
 
  /**
    * ID card length.
    *
    * @return
    */
  public int getLength(String str) {
    return () ? 0 : ();
  }
 
  /**
    * Whether it is a 15-digit ID card.
    *
    * @return true: 15-bit false: others.
    */
  public boolean is15() {
    return () == 15;
  }
 
  /**
    * Whether it is an 18-digit ID card.
    *
    * @return true: 18-bit false: others.
    */
  public boolean is18() {
    return () == 18;
  }
 
  /**
    * Get the province code of the ID card.
    *
    * @return Province code.
    */
  public String getProvince() {
    return () == 0 ? (0, 2) : "";
  }
 
  /**
    * Get the city code of your ID card.
    *
    * @return City code.
    */
  public String getCity() {
    return () == 0 ? (2, 4) : "";
  }
 
  /**
    * The district and county code for obtaining the ID card.
    *
    * @return District and County Code.
    */
  public String getCountry() {
    return () == 0 ? (4, 6) : "";
  }
 
  /**
    * The year of birth when you get your ID card.
    *
    * @return Year of birth.
    */
  public String getYear() {
    if (() != 0)
      return "";
 
    if (() == 15) {
      return "19" + (6, 8);
    } else {
      return (6, 10);
    }
  }
 
  /**
    * The month of birth when you get your ID card.
    *
    * @return Month of birth.
    */
  public String getMonth() {
    if (() != 0)
      return "";
 
    if (() == 15) {
      return (8, 10);
    } else {
      return (10, 12);
    }
  }
 
  /**
    * The date of birth when you get your ID card.
    *
    * @return Date of birth.
    */
  public String getDay() {
    if (() != 0)
      return "";
 
    if (() == 15) {
      return (10, 12);
    } else {
      return (12, 14);
    }
  }
 
  /**
    * Date of birth when you get your ID card.
    *
    * @return Date of birth.
    */
  public String getBirthday() {
    if (() != 0)
      return "";
 
    if (() == 15) {
      return "19" + (6, 12);
    } else {
      return (6, 14);
    }
  }
 
  /**
    * The date of birth when you get your ID card.
    *
    * @return Date of birth.
    */
  public String getBirthMonth() {
    return getBirthday().substring(0, 6);
  }
 
  /**
    * Get the sequence number of your ID card.
    *
    * @return Sequence number.
    */
  public String getOrder() {
    if (() != 0)
      return "";
 
    if (() == 15) {
      return (12, 15);
    } else {
      return (14, 17);
    }
  }
 
  /**
    * Get gender.
    *
    * @return Gender: 1-Male 2-Female
    */
  public String getSex() {
    if (() != 0)
      return "";
 
    int p = (getOrder());
    if (p % 2 == 1) {
      return "male";
    } else {
      return "female";
    }
  }
 
  /**
    * Get the gender value.
    *
    * @return Gender: 1-Male 2-Female
    */
  public String getSexValue() {
    if (() != 0)
      return "";
 
    int p = (getOrder());
    if (p % 2 == 1) {
      return "1";
    } else {
      return "2";
    }
  }
 
  /**
    * Get the check digit.
    *
    * @return check bit.
    */
  public String getCheck() {
    if (!())
      return "";
 
    String lastStr = (() - 1);
    if ("x".equals(lastStr)) {
      lastStr = "X";
    }
    return lastStr;
  }
 
  /**
    * Get a 15-digit ID card.
    *
    * @return 15-digit ID card.
    */
  public String to15() {
    if (() != 0)
      return "";
 
    if (this.is15())
      return ;
    else
      return (0, 6) + (8, 17);
  }
 
  /**
    * Get an 18-digit ID card.
    *
    * @return 18-digit ID card.
    */
  public String to18() {
    if (() != 0)
      return "";
 
    if (this.is18())
      return ;
    else
      return (0, 6) + "19" + (6) + ();
  }
 
  /**
    * Get an 18-digit ID card.
    *
    * @return 18-digit ID card.
    */
  public static String toNewIdCard(String tempStr) {
    if (() == 18)
      return (0, 6) + (8, 17);
    else
      return (0, 6) + "19" + (6) + getCheckBit(tempStr);
  } 
  /**
    * Verify that the ID card is correct
    *
    * @return 0: Correct
    */
  public int isCorrect() {
    if (()) {
       = IdCardUtil.IS_EMPTY;
      return ;
    }
 
    if (!()) {
       = IdCardUtil.LEN_ERROR;
      return ;
    }
 
    if (!()) {
       = IdCardUtil.CHAR_ERROR;
      return ;
    }
 
    if (!()) {
       = IdCardUtil.DATE_ERROR;
      return ;
    }
 
    if (this.is18()) {
      if (!().equals(())) {
         = IdCardUtil.CHECK_BIT_ERROR;
        return ;
      }
    } 
    return 0;
  } 
 
  private boolean isLenCorrect() {
    return this.is15() || this.is18();
  }
 
  /**
    * Determine whether the date of birth in the ID card is correct.
    *
    * @return
    */
  private boolean isDateCorrect() {
 
    /*Non-leap year days*/
    int[] monthDayN = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    /*Number of days in leap year*/
    int[] monthDayL = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
    int month;
    if (this.is15()) {
      month = ((8, 10));
    } else {
      month = ((10, 12));
    }
 
    int day;
    if (this.is15()) {
      day = ((10, 12));
    } else {
      day = ((12, 14));
    }
 
    if (month > 12 || month <= 0) {
      return false;
    }
 
    if (()) {
      if (day > monthDayL[month - 1] || day <= 0)
        return false;
    } else {
      if (day > monthDayN[month - 1] || day <= 0)
        return false;
    }
 
    return true;
  }
 
  /**
    * Get the check digit.
    *
    * @return
    */
  private String getCheckBit() {
    if (!())
      return "";
 
    String temp = null;
    if (this.is18())
      temp = ;
    else
      temp = (0, 6) + "19" + (6); 
 
    String checkTable[] = new String[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
    int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    int sum = 0;
 
    for (int i = 0; i < 17; i++) {
      String ch = (i, i + 1);
      sum = sum + (ch) * wi[i];
    } 
    int y = sum % 11; 
    return checkTable[y];
  } 
 
  /**
    * Get the check digit.
    *
    * @return
    */
  private static String getCheckBit(String str) {
 
    String temp = null;
    if (() == 18)
      temp = str;
    else
      temp = (0, 6) + "19" + (6); 
 
    String checkTable[] = new String[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
    int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    int sum = 0;
 
    for (int i = 0; i < 17; i++) {
      String ch = (i, i + 1);
      sum = sum + (ch) * wi[i];
    } 
    int y = sum % 11; 
    return checkTable[y];
  } 
 
  /**
    * Whether there are illegal characters in the ID number.
    *
    * @return true: Correct false: Illegal characters exist.
    */
  private boolean isCharCorrect() {
    boolean iRet = true;
 
    if (()) {
      byte[] temp = ();
 
      if (this.is15()) {
        for (int i = 0; i < ; i++) {
          if (temp[i] < 48 || temp[i] > 57) {
            iRet = false;
            break;
          }
        }
      }
 
      if (this.is18()) {
        for (int i = 0; i < ; i++) {
          if (temp[i] < 48 || temp[i] > 57) {
            if (i == 17 && temp[i] != 88) {
              iRet = false;
              break;
            }
          }
        }
      }
    } else {
      iRet = false;
    }
    return iRet;
  }
 
  /**
    * Determine whether the birth year of the ID card is not a leap year.
    *
    * @return true : leap year false flat year
    */
  private boolean isLeapyear() {
    String temp;
 
    if (this.is15()) {
      temp = "19" + (6, 8);
    } else {
      temp = (6, 10);
    }
 
    int year = (temp);
 
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
      return true;
    else
      return false;
  }
}

What is important is that this method can verify "X", "x".

The above case of Android ID number validity verification tool is all the content I share with you. I hope you can give you a reference and I hope you can support me more.