This ID tool class has the following features:
1, verify the identity card number is legal. (16-bit or 18-bit)
2. Calculate age by ID number.
3. The date of birth is calculated by ID number
4. Gender calculation by ID number
PackageCom.ciming.util;ImportJava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;ImportJava.util.Date; Public class idcardutil { Private Final Static int[] WI = {7,9,Ten,5,8,4,2,1,6,3,7,9,Ten,5,8,4,2,1};Private Final Static int[] VI = {1,0,' X ',9,8,7,6,5,4,3,2};Private Static int[] ai =New int[ -];/** * Verify ID * * @param idcard * @return * * Public Static Boolean Verifyidcard(String Idcard) {if(idcard.length () = = the) {Idcard = Uptoeighteen (Idcard); }if(Idcard.length ()! = -) {return false; } String Verify = Idcard.substring ( -, -);if(Verify.equals (Getverify (Idcard))) {return true; }return false; }Private StaticStringgetverify(String Eightcardid) {intRemaining =0;if(eightcardid.length () = = -) {Eightcardid = Eightcardid.substring (0, -); }if(eightcardid.length () = = -) {intsum =0; for(inti =0; I < -; i++) {String k = eightcardid.substring (i, i +1); Ai[i] = Integer.parseint (k); } for(inti =0; I < -; i++) {sum = sum + wi[i] * Ai[i]; } remaining = sum% One; }returnRemaining = =2?"X": String.valueof (vi[remaining]); }Private StaticStringUptoeighteen(String Fifteencardid) {String Eightcardid = fifteencardid.substring (0,6); Eightcardid = Eightcardid +"very"; Eightcardid = Eightcardid + fifteencardid.substring (6, the); Eightcardid = Eightcardid + getverify (eightcardid);returnEightcardid; }/** * Get sex * * @param idcard * @return * * Public StaticStringGetsex(String Idcard) {String inputstr = idcard.tostring ();intSexif(inputstr.length () = = -) {sex = Inputstr.charat ( -);if(Sex%2==0) {return "female"; }Else{return "Male"; } }Else{sex = Inputstr.charat ( -);if(Sex%2==0) {return "female"; }Else{return "Male"; } } }/** * Get Birth date * * @param idcard * @return * @throws parseexception */ Public StaticDategetbirthdate(String Idcard)throwsParseException {String year; String month; String Day;if(idcard.length () = = -) {//handling 18-digit IDYear = idcard.substring (6,Ten); month = idcard.substring (Ten, A); Day = idcard.substring ( A, -); }Else{//processing non-18-digit ID cardYear = idcard.substring (6,8); month = idcard.substring (8,Ten); Day = idcard.substring (Ten, A); Year ="very"+ year; }return NewSimpleDateFormat ("Yyyy-mm-dd"). Parse (year +"-"+ Month +"-"+ day); }/** * @throws parseexception * * Based on ID card * * * Public Static int Getage(String Idcard)throwsparseexception {Calendar Calbirth = calendar.getinstance (); Calendar today = Calendar.getinstance (); Date birthDate = getbirthdate (Idcard); Calbirth.settime (birthDate);returnGetyeardiff (today, Calbirth); } Public Static int Getyeardiff(Calendar cal, calendar Cal1) {intm = (Cal.get (Cal. MONTH)-(Cal1.get (CAL1). MONTH));inty = (Cal.get (Cal. Year)-(Cal1.get (CAL1). year));return(Y * A+ M)/ A; } Public Static void Main(string[] args) { }}
Java ID Validation Tool class