Java IDカードの合法性の確認と、参照のためにIDカード番号の有効な情報を取得します。特定のコンテンツは次のとおりです
Java IDカードの合法性検証
/** IDカードの最初の6桁[ABCDEF]は、管理部門のデジタルコードです(略してデジタルコードを参照)説明(「GB/T 2260-2007中国共和国の管理部コード」を参照):*このデジタルコードの編集の原則と構造分析は、3階建ての領域を採用しています。中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国の中国によると、中国の郡(地域、自律県、リーグ)、および郡(地域、自治県、リーグ)、および郡(自治郡、郡レベルの都市)、および都市(地域、自律県、リーグ)、および都市(地域、自治県)、都市(地域、自律県)、左から右へのデジタルコードコード構造の意味は次のとおりです。最初のレイヤーは、州、自治地域、中央政府の直下の自治体を表すAB 2桁のコード、および特別な行政区です。 2番目のレイヤーは、中央政府の直接の自治体、地域、地域、自治県、同盟、および自治体の要約コードを表すCD 2桁のコードと、郡レベルの行政部門の直接の地方(自治地域)の概要コードです。 - 21〜50は、地区の要約コード、自治県、同盟を表しています。 --90は、州(自律領域)の直下に郡レベルの行政部門の要約コードを表します。 3階は、郡、自治郡、郡レベルの都市、バナー、自律バナー、市の地区、森林地帯、特別地区を表す2つのEF桁です。その中には、都市の管轄下にある郡レベルの都市を持つ郡レベルの都市、地区(自治フレクタ、リーグ)、および郡レベルの行政部門の直下にある郡レベルの都市(自治区(自治地域)の郡レベルの都市)を表しています。 01は通常、管轄権の要約コードを表します。 - 21〜80は、郡、自治郡、バナー、自律バナー、森林地帯、地域の特別地区の管轄下にある郡レベルの都市を表しています。 - 81〜99は、州の管轄下にある郡レベルの都市(自律地域)を表しています。 */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.util.util.util.util.util.util.util.util.util. <p>クラスの説明:IDカードの合法性の確認 * </p> * <p> * -15桁のID番号:7桁目と8桁目は出生年(2桁)、9桁目と10桁は生年月日と12桁目と12桁目が生年月日を表し、15桁は性別を表し、偶数数は女性です。 * -18桁のID番号*:7、8、9、10は出生年(4桁)、11、12は生年月、13と14は生年月日を表し、17は性別を表し、奇数は男性であり、偶数は女性です。 * </p> */@suppresswarnings({"un -checked"、 "unsused"、 "all"})public class idcardvalidator {/** *地方、市のコードテーブル:{11: "Beijing"、12: "Tianjin"、13: "hebei"、14: "shanxi"、15: " 22:「Jilin」、23: "heilongjiang"、31: "shanghai"、32: "jiangsu"、 * 33: "Zhejiang"、34: "anhui"、35: "fujian"、36: "jiangxi"、37: "shandong"、41: " 42: "Hubei"、43: "Hunan"、44: "Guangdong"、45: "Guangxi"、46: "Hainan"、50: "Chongqing"、 * 51: "sichuan"、52: "guizhou"、53: "Yunnan"、54: "tibet"、61: "shaanxi"、 63:「Qinghai」、64: "Ningxia"、65: "Xinjiang"、71: "Taiwan"、81: "Hong Kong"、82: "Macao"、91: "foreign"} */保護された文字列codeandcity [] = {{{{"" "13"、 "hebei"}、{"14"、 "shanxi"}、{"15"、 "Inner Mongolia"}、{"21"、 "liaoning"}、{"22"、 "jilin"}、{"23"、 "heilongjiang"}、{"31" "" 32 "" 32 "、 }、{"33"、 "Zhejiang"}、{"34"、 "anhui"}、{"35"、 "Fujian"}、{"36"、 "jiangxi"}、{"37"、 "shandong"}、{"" " "43"、 "Hunan"}、{"44"、 "Guangdong"}、{"45"、 "Guangxi"}、{"46"、 "hainan"}、{"50"、 "Chongqing"}、{"51"、 "sichuan" "Yunnan"}、{"54"、 "Tibet"}、{"61"、 "shaanxi"}、{"62"、 "gansu"}、{"63"、 "Qinghai"}、{"64"、 "ningxia"}、{"65"、 "xiniang" "" " }、{"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" 43 "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 "}; / ** *すべてのIDカードの合法性を確認します * * @param idcard * @return */ public boolean isvalidatedallidcard(string idcard){if(idcard.length()== 15){idcard = this.convertidcarby15bit(idcard); } this.isvalidate18idcard(idcard); } /** * <p> * 18桁のIDカードの合法性を決定します * < /p> *中国共和国の国家標準GB11643-1999の市民権ID番号に関する規定に従って、市民権ID番号は、17桁のデジタルオントロジーコードと1桁の解決コードで構成される機能の組み合わせコードです。 *配置順序は左から右にあります:6桁のアドレスコード、8桁の生年月日コード、3桁のシーケンスコード、1桁の検証コード。 * <p> *シーケンスコード:同じアドレスコードで識別されたエリア内の同じ年、月、日に生まれた人に割り当てられたシーケンス番号を示します。シーケンスコードの奇数数は男性に割り当てられ、偶数が女性に割り当てられます。 * </p> * <p> * 1。最初の1桁と2桁は次のものを表します。州のコード。 2。3桁と4桁目は次のことを表します。市のコード。 3. 5番目と6桁目は次のとおりです。地区と郡のコード。 *4。7桁から14桁目は、次のことを表しています。 5. 15桁目と16桁目は次のとおりです。場所の警察署のコード。 * 6。17桁目は性別を表します。奇数は男性を表し、偶数は女性を表します。 *7。18桁目は校正コードです。一部の人は、個人情報コードであり、一般にIDカードの正確性を確認するためにコンピューターによってランダムに生成されると言う人もいます。キャリブレーションコードは0から9までの数になり、xで表される場合もあります。 * </p> * <p> * 18桁の計算方法(チェックコード)は次のとおりです。1。以前のID番号の17桁の数に異なる係数を掛けます。最初の数字から17桁目までの係数は次のとおりです。7910 5 8 4 * 2 1 6 3 7 9 10 5 8 4 2 * </p> * <p> * 2。これらの17桁の数値と係数を増やした結果を追加します。 * </p> * <p> * 3。追加を使用して11で割って残りが何であるかを確認しますか? * </p> * 4。残りは11の数字のみを持つことができます0 1 2 3 4 5 6 7 8 9 10。それぞれに対応する最後のIDカードの数は1 0 x 9 8 7 6 5 4 3 * 2です。残りが10の場合、IDカードの最後の数は2です。 * </ p> * * @param idcard * @return */ public boolean isvalidate18idcard(string idcard){// non-18ビットはfalse(idcard.length()!= 18){return false; } //最初の17ビット文字列IDcard17 = idcard.substring(0、17)を取得します。 // 18番目の文字列idcard18code = idcard.substring(17、18)を取得します。 char c [] = null;文字列checkcode = ""; //すべての数字です(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){falseを返します。 } // IDカードの18番目のビットを計算されたキャリブレーションコードと一致させます。等しくない場合、(!idcard18code.equalsignorecase(checkcode)){return false; }} trueを返します。 } /*** 15ビットIDカードの合法性を確認します。この方法は不正確です。このクラスで提供されている審査の前に、15ビットから18ビットを変換するのが最善です。 * * @param idcard * @return */ public boolean isvalidate15idcard(string idcard){//非15ビットはfalseです(idcard.length()!= 15){return false; } //すべての数字ですif(isdigital(idcard))){string provenceid = idcard.substring(0、2);文字列誕生日= 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)); //それが合法的な州であるかどうかを判断します。 for(string id:citycode){if(id.equals(provenceid)){flag = true;壊す; }} if(!flag){return false; } //このIDカードの生年月日は、現在の日付の日付の誕生日= nullの後に偽です。 try {birthdate = new simpledateFormat( "yymmdd")。parse(Birthday); } catch(parseexception e){e.printstacktrace(); } if(birthdate == null || new date()。前(誕生日)){return false; } // GregorianCalendar Curday = new GregorianCalendar()であるかどうかを判断するには; int curyear = curday.get(calendar.year); int year2bit = integer.parseint(string.valueof(curyear).substring(2)); //} //(月<1 ||月> 12){return false; } //それが法的日付であるかどうかを判断するためにboolean mflag = false; curday.settime(誕生日); //このIDカードの生年月日をオブジェクトCurday Switch(月)に割り当てる{ケース1:ケース3:ケース5:ケース7:ケース10:ケース12:MFLAG =(day> = 1 && day <= 31);壊す;ケース2://グレゴリオカレンダーの2月には28日間、LEAP年の2月に29日があります。 if(curday.isleapyear(curday.get(calendar.year))){mflag =(day> = 1 && day <= 29); } else {mflag =(day> = 1 && day <= 28); } 壊す;ケース4:ケース6:ケース9:ケース11:mflag =(day> = 1 && day <= 30);壊す; } if(!mflag){return false; }} else {return false; } trueを返します。 } / ** * 15桁のIDカードを18桁のIDカードに変換します * * @Param idcard * @return * / public String convertidcarby15bit(string idcard){string idcard17 = null; //非15ビットIDカードif(idcard.length()!= 15){return null; } if(isdigital(idcard)){//生年月日の日付を取得します誕生日= idcard.substring(6、12);日付の誕生日= null; try {birthdate = new simpledateFormat( "yymmdd")。parse(Birthday); } catch(parseexception e){e.printstacktrace(); }カレンダーcday = charend.getInstance(); cday.settime(誕生日); string year = string.valueof(cday.get(calendar.year)); idcard17 = idcard.substring(0、6) + year + idcard.substring(8); char c [] = idcard17.tochararray();文字列checkcode = ""; if(null!= c){int bit [] = new int [idcard17.length()]; //文字配列を整数配列に変換ビット= converchartoint(c); int sum17 = 0; sum17 = getPowersum(bit); //合計値を取得し、残りを取得してコードCheckCode = getCheckCodeBysum(SUM17)を確認します。 //チェックビットを取得できませんif(null == checkcode){return null; } // 18桁のチェックコードIDCard17 += CheckCodeで最初の17桁をスプライスします。 }} else {// IDカードには番号return nullが含まれています。 } return idcard17; } / ** * 15ビットおよび18ビットIDカード番号の基本的な数値および数字の検証 * * @param idcard * @return * / public boolean isidcard(string idcard){return idcard == null || ""。quals(idcard)? false:pattern.matches( "(^// d {15} $)|(// d {17}(?:// d | x | x)"、idcard); } / ** *基本的な数字と数字15桁のID番号のチェック * * @param idcard * @return * / public boolean is15idcard(string idcard){return idcard == null || ""。quals(idcard)? false:pattern.matches( "^[1-9] // d {7}(0 // d)|(1 [0-2])))(([0 | 1 | 2] // d)| 3 [1])// d {3} $"、idcard); } / ** *基本番号と数字18桁のID番号のチェック"^[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]*$"); } / ** * IDカードの各ビットと対応するビットの重み係数を掛けてから、合計値を取得します * * @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 check bit * / public string getcheckcodebysum(int sum17){string checkcode = null; switch(sum17%11){ケース10:checkcode = "2";壊す;ケース9:CheckCode = "3";壊す;ケース8:CheckCode = "4";壊す;ケース7:CheckCode = "5";壊す;ケース6:CheckCode = "6";壊す;ケース5:CheckCode = "7";壊す;ケース4:CheckCode = "8";壊す;ケース3:CheckCode = "9";壊す;ケース2:CheckCode = "x";壊す;ケース1:CheckCode = "0";壊す;ケース0:CheckCode = "1";壊す; } 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; } public static void main(string [] args)throws exception {string idcard15 = "142431199001145"; // 15ビット文字列idcard18 = "121212121212121212121212121212121212121212121212"; // 18ビットidcardator iv = new idcardvalidator(); System.out.println(iv.isvalidatedallidcard(idcard15)); System.out.println(iv.isvalidatedallidcard(idcard18)); }}IDカード番号に基づいてIDカードに関連する情報を抽出する
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; idcardinfoextractor {//州のプライベートストリング州; //市のプライベートストリングシティ。 //地区および郡のプライベートストリング地域。 //年間int year; //月Private int Month; //日付プライベートインターデイ; //ジェンダープライベートストリングジェンダー。 //生年月日誕生日の日付。プライベートマップ<文字列、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"、 "インナーモンゴル"); this.put( "21"、 "liaoning"); this.put( "22"、 "Jilin"); this.put( "23"、 "heilongjiang"); this.put( "31"、 "上海"); 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"、 "台湾"); this.put( "81"、 "Hong Kong"); this.put( "82"、 "macao"); this.put( "91"、 "Foreign"); }}; private idcardvalidator validator = null; / *** constructorを介して各メンバー属性を初期化*/ public idcardinfoextractor(string idcard){try {validator = new idcardvalidator(); if(validator.isvalidatedallidcard(idcard)){if(idcard.length()== 15){idcard = balidator.convertidcarby15bit(idcard); } // sting string provenceid = idcard.substring(0、2)を取得します。 <string> key = this.citycodemap.keyset(); for(string id:key){if(id.equals(provenceid)){this.province = this.citycodemap.get(id);壊す; }} //性別文字列を取得するid17 = idcard.substring(16、17); if(integer.parseint(id17)%2!= 0){this.gender = "male"; } else {this.gender = "female"; } //生年月日の日付を取得します誕生日= idcard.substring(6、14); date birthdate = new simpledateFormat( "yyyymmdd").parse(誕生日); this.birthday = Birthday; 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(例外e){e.printstacktrace(); }} / ** * @return the州 * / public String getProvince(){return vevince; } / ** * @return the City * / public String getCity(){return City; } / ** * @return the Region * / public string getRegion(){return region; } / ** * @return the year * / public intyear(){return year; } / ** * @return the month * / public intmonth(){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 "untion:" + this.province + "、gender:" + this.gender + "、生年月日:" + this.birthday; } public static void main(string [] args){string idcard = "1212121212121212121212"; idcardinfoextractor ie = new idcardinfoextractor(idcard); System.out.println(ie.toString()); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。