Verify the validity of the Java ID card and extract the valid information _java

Source: Internet
Author: User

The validity of the Java ID Card check and obtain the ID number valid information for your reference, the specific contents are as follows

Verification of the validity of Java ID card

/** ID card before 6 "ABCDEF" for the administrative division of the digital Code (referred to as "GB/T 2260-2007 Code of Administrative division of the People's Republic of China"): * The number of coding principles and structural analysis, it uses three-tier six-bit hierarchical code structure, 
 According to the level of each of our provinces (autonomous regions, municipalities, Special Administrative Region), * City (region, Autonomous Prefecture, AU), counties (autonomous counties, county level, flag, autonomous flag, municipal district, forest area, SAR). The meaning of the digital code bit structure from left to right is: the first layer for the AB two-bit code represents the provinces, autonomous regions, municipalities and special administrative regions; The second layer is CD two-bit code indicates city, region, Autonomous Prefecture, AU, municipalities under the jurisdiction of District, county summary Code, Province (autonomous Region) of the total county level of administrative division, including:--01~20 
 51~70 said that the city, 01, 02 is also used to express municipalities under the jurisdiction of the District, county summary code,--21~50 said the region, Autonomous Prefecture, AU,--90 said province (autonomous region) of the county-level administrative division of the total code. The third layer is the EF two-bit representing counties, autonomous counties, county level counties, flags, autonomous flags, municipal districts, forest regions and special zones, including:--01~20 said that the municipal district, the region (Autonomous Prefecture, the Union) jurisdiction of counties, municipalities, and provinces (autonomous regions) of the prefecture-level administrative divisions of the city, 01 usually represents the area summary code 
 80 means counties, autonomous counties, flags, autonomous flags, forest areas and regions, and--81~99 said that the province (autonomous region) governs county.
* * Import java.text.ParseException;
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.regex.Pattern; /** * <p> * class Description: ID card Legality verification * </p> * <p> *-15 ID Card number: 7th, 8 is the year of birth (two digits), 9th, 10 is the birth month, 11th, 12 representative Birth date, 15th representative
 Sex, odd for male, even for female. *-18 ID Number *: 7th, 8, 9, 10 for the Year of birth (four digits), 11th, 12th for the birth of the month, 13th, 14 to represent the date of birth, 17th bit representative, odd for male,The even number is female. * </p>/@SuppressWarnings ({"Unchecked", "unused", "all"}) public class Idcardvalidator {/** * province, central Municipality code table: {1 1: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", * 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", * 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", * 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", * 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi" , 62: "Gansu", * 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"}/protected String codeandcity[][] = {"11", "North Beijing "}, {" 12 "," Tianjin "}, {" 13 "," Hebei "}, {" 14 "," Shanxi "}, {" 15 "," Inner Mongolia "}, {" 21 "," Liaoning "}, {" 22 "," Jilin "}, {" 23 "," Heilongjiang "} , {"31", "Shanghai"}, {"32", "Jiangsu"}, {"33", "Zhejiang"}, {"34", "Anhui"}, {"35", "Fujian"}, {"36", "Jiangxi"}, {"37", "Shandong"}, {"4" 1 "," Henan "}, {" 42 "," Hubei "}, {" 43 "," Hunan "}, {" 44 "," Guangdong "}, {" 45 "," Guangxi "}, {" 46 "," Hainan "}, {" 50 "," Chongqing "}, {" 51 "," four
  Chuan "}, {" 52 "," Guizhou "}, {" 53 "," Yunnan "}, {" 54 "," Tibet "}, {" 61 "," Shaanxi "}, {" 62 "," Gansu "}, {" 63 "," Qinghai "}, {" 64 "," Ningxia "}, {"65", "Xinjiang"}, {"71", "Taiwan"}, {"81", "Hong Kong"}, {"82", "Macao"}, {"91", "Foreign"}}; 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"

 };

 Per-weighted factor private int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

 18th-digit School Check code private String verifycode[] = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}; /** * Verify the legality of all IDs * * @param idcard * @return/public boolean isvalidatedallidcard (String idcard) {if Idcard.
 Length () = = {Idcard = This.convertidcarby15bit (Idcard);
 Return This.isvalidate18idcard (Idcard); /** * <p> * To determine the legality of the 18 ID card * </p> * According to the national standard Gb11643-1999〗 of the People's Republic of China on the civil status number, the citizenship number is a feature combination code, by 17-digit body code and a digital school
 Inspection code composition.
 * Order from left to right: six-digit address code, eight-digit birth date code, three-digit sequence code and a digital parity code.
 * <p> * Sequential code: means that the same address code identified in the area, the same year, the same month, born on the same day, the order number of the odd distribution of order code to men, even assigned to women. *;/p> * <p> * 1. The first 1, 2 digits indicate: Code of the province in which it is located; 2. The 3rd, 4 digits indicate: The code of the city; 3.5th, 6 digits indicate: The code of the county; * 4.7th to 14th Digit indicates: Year of birth, month, day; 5.15th, 16 digits Word: The location of the police station code; * 6. The 17th digit indicates gender: The odd number denotes a male, and the number is a female; * 7. The 18th digit is the school check code: Also some say is the personal information code, is usually randomly generated with the computer, used to check the correctness of ID
 The school check code can be a 0~9 number, sometimes also expressed in X. * </p> * <p> * 18th digit (parity code) calculation method is: 1. The previous ID number 17 digits are multiplied by different coefficients respectively.
 The coefficients from the first to the 17th position are: 7 9 5 8 4 * 2 1 6 3 7 9 5 8 4 2 * * </p> * <p> * 2. Adds the results of this 17-digit number and the factor multiplication.
 * </p> * <p> * 3. Add out and divide by 11 to see what the remainder is? * </p> * 4. The remaining number may only have 0 1 2 3 4 5 6 7 8 9 10 of these 11 digits.
 The number of the last ID card corresponding to the respective is 1 0 X 9 8 7 6 5 4 3 * 2. * <p> * 5. It is learned that if the remainder is 2, the Ⅹ of Roman numerals will appear on the 18th digit of the ID card.
 If the remainder is 10, the last number of the ID card is 2. * </p> * * @param idcard * @return/public boolean isvalidate18idcard (String idcard) {//non 18-bit is False if (Idcar
 D.length ()!= {return false;
 //Get the first 17-bit String idcard17 = idcard.substring (0, 17);
 Gets the 18th-bit String Idcard18code = idcard.substring (17, 18);
 char c[] = null;
 String Checkcode = ""; is the number 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);
  The Checkcode = Getcheckcodebysum (SUM17) is determined by verifying the remainder of the sum and the value and the 11 modulo;
  if (null = = Checkcode) {return false;
  ///Match the 18th digit of the ID card with the calculated school code, not equal to the false if (!idcard18code.equalsignorecase (Checkcode)) {return false;
 } return true;
 /** * Verify the legality of the 15-bit ID, the method is not accurate, it is best to convert 15 to 18 digits before judging, which is provided in the class. * * @param idcard * @return/public boolean isvalidate15idcard (String idcard) {//Non 15-bit is False if (Idcard.length ()!=
 {return false;
  //Whether all are numeric 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));
  A province Boolean flag = False that determines whether it is legal; for (String id:cItycode) {if (Id.equals (Provinceid)) {flag = true;
  Break
  } if (!flag) {return false;
  //The ID date is false date birthdate = null after the current date;
  try {birthdate = new SimpleDateFormat ("YyMMdd"). Parse (birthday);
  catch (ParseException e) {e.printstacktrace ();
  } if (birthdate = null | | | new Date (). Before (birthdate)) {return false;
  //Determine if the year is valid GregorianCalendar Curday = new GregorianCalendar ();
  int curyear = Curday.get (calendar.year);

  int year2bit = Integer.parseint (string.valueof (curyear). SUBSTRING (2));
  The two-bit notation for determining the year, less than 50 and greater than the current year, is False if ((Year < && year > Year2bit)) {return false;
  //Determine if the legal month if (Month < 1 | | month >) {return false;
  //Determine if the date is valid Boolean mflag = false; Curday.settime (birthdate);  Assign the date of birth of the ID to the object 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://The February non-leap year of the Gregorian calendar has 28 days, and the February of the leap year is 29 days.
  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; /** * Convert 15 ID card to 18 ID * * @param idcard * @return/public string convertidcarby15bit (string idcard) {Strin
 G idcard17 = null;
 Non 15-bit ID card if (idcard.length ()!=) {return null;
  } if (Isdigital (Idcard)) {//Get Birth date 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 ()];
  Converts an array of characters to an integer array bit = Converchartoint (c);
  int sum17 = 0;

  Sum17 = getpowersum (bit);
  Get and value and 11 modulo get the remainder for the check code Checkcode = Getcheckcodebysum (SUM17);
  Cannot get the checksum bit if (null = = Checkcode) {return null;
  ///Place the top 17 with the 18th-bit parity idcard17 + = Checkcode;
 } else {//ID card contains number return null;
 return idcard17; /** * 15-bit and 18-digit identification number basic numbers and digits of the school * * @param idcard * @return/public boolean isidcard (String idcard) {return ID Card = = NULL | | "". Equals (Idcard)? False:Pattern.matches ("(^\\d{15}$) | ( \\D{17} (?: \ \d|x|
 X) $) ", Idcard); /** * 15 ID number Basic number and number of Idcard * * @param * * @return/public boolean is15idcard (String idcard) {return IDCA RD = 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 ID number of basic digits and digits of the school * * @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); /** * Digital Verification * @param STR * @return/public boolean isdigital (String str) {return str = NULL | | ". Equals (str)?
 False:str.matches ("^[0-9]*$"); 

 /** * Multiplies each digit of the ID card and the weighted factor of the corresponding bit, and then obtains and the value * * @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; /** * Will and value and 11 modulo to get the remainder of the check code to judge * * @param checkcode * @param sum17 * @return Check bit/public String getcheckcodebysum (int sum17)
 {String checkcode = null;
  Switch (sum17%) {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; /** * Converts an array of characters to an integral array * * @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; public static void Main (string[] args) throws Exception {string idcard15 = "142431199001145";//15-bit String idcard1
 8 = "121212121212121212";//18-bit Idcardvalidator IV = new Idcardvalidator ();
 System.out.println (Iv.isvalidatedallidcard (IDCARD15));
 
 System.out.println (Iv.isvalidatedallidcard (IDCARD18));

 }
}

Collect ID card information according to ID number

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;
 /** * <p> * Class Description: Extract identity card Related information * </p>/public class Idcardinfoextractor {//province private String province;
 The city is private;
 Counties private String region;
 years private int year;
 month private int month;
 Date private int day;
 Gender private String gender;

 Date of birth private date birthday;
  Private map<string, string> citycodemap = new hashmap<string, string> () {{this.put ("11", "Beijing");
  This.put ("12", "Tianjin");
  This.put ("13", "Hebei");
  This.put ("14", "Shanxi");
  This.put ("15", "Inner Mongolia");
  This.put ("21", "Liaoning");
  This.put ("22", "Jilin");
  This.put ("23", "Heilongjiang");
  This.put ("31", "Shanghai");
  This.put ("32", "Jiangsu");
  This.put ("33", "Zhejiang");
  This.put ("34", "Anhui");
  This.put ("35", "Fujian");
  This.put ("36", "Jiangxi");
  This.put ("37", "Shandong");
  This.put ("41", "Henan"); This.pUT ("42", "Hubei");
  This.put ("43", "Hunan");
  This.put ("44", "Guangdong");
  This.put ("45", "Guangxi");
  This.put ("46", "Hainan");
  This.put ("50", "Chongqing");
  This.put ("51", "Sichuan");
  This.put ("52", "Guizhou");
  This.put ("53", "Yunnan");
  This.put ("54", "Tibet");
  This.put ("61", "Shaanxi");
  This.put ("62", "Gansu");
  This.put ("63", "Qinghai");
  This.put ("64", "Ningxia");
  This.put ("65", "Xinjiang");
  This.put ("71", "Taiwan");
  This.put ("81", "Hong Kong");
  This.put ("82", "Macao");
 This.put ("91", "foreign");

 }
 };

 Private Idcardvalidator validator = null;
  /** * Initialize each member property by constructing the method/public Idcardinfoextractor (String idcard) {try {validator = new idcardvalidator (); if (Validator.isvalidatedallidcard (Idcard)) {if (idcard.length () =) {Idcard = Validator.convertidcarby15bit (IDC
  ARD);
  //Get the province 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 }//Get Gender StrinG Id17 = idcard.substring (16, 17);
  if (Integer.parseint (id17)% 2!= 0) {this.gender = "male";
  else {This.gender = "female";
  //Get date of birth 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 String Getgender () {return gender;
 }/** * @return The birthday */public Date Getbirthday () {return birthday; @Override public String toString () {return "province: + this.province +", Sex: "+ This.gender +", Date of birth: "+ This.birthda
 Y
 public static void Main (string[] args) {String Idcard = "121212121212121212";
 Idcardinfoextractor ie = new Idcardinfoextractor (Idcard);
 System.out.println (Ie.tostring ());
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.