Recently made a system, involving the user entered the identification number of the verification, reduce dirty data into the background processing and reduce the cost of enterprise verification, so in the access layer on the input information to do a strong format check.
Directly attached to the code, can be used directly.
1 PackageHope.identitycodecheck.demo;2 3 ImportJava.text.DateFormat;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 /**7 * 8 * @authorHP9 * Ten * */ One Public classIdentitycodeutil { A /** - * ID Number Check (18-bit support) - * the * */ - Public Static BooleanCheckidentitycode (String identitycode) { - if(!identitycode.matches ("\\d{17}" (\\d|x| X) $ ")) { - return false; + } -Date d =NewDate (); +DateFormat DF =NewSimpleDateFormat ("YyyyMMdd"); A intYear =Integer.parseint (Df.format (d)); at if(Integer.parseint (identitycode.substring (6)) < 1900 | | Integer.parseint (identitycode.substring (6, ten)) > year) {//7-10-bit is the year of birth, and the range should be between 1900-the current year - return false; - } - if(Integer.parseint (Identitycode.substring (Ten)) < 1 | | Integer.parseint (Identitycode.substring (Ten)) > 12) {//11-12 digits represent the month of birth, the range should be between 01-12 - return false; - } in if(Integer.parseint (Identitycode.substring ()) < 1 | | Integer.parseint (Identitycode.substring ()) > 31) {//13-14-bit is the date of birth, the range should be between 01-31 - return false; to } + //check 18th bit - //S = SUM (Ai * Wi), i = 0, ..., 16, the first 17 digits of the right sum the //Ai: A numeric value representing the ID number at position i * //Wi: Indicates the weighting factor at position I $ //Wi:7 9 5 8 4 2 1 6 3 7 9 Ten 5 8 4 2Panax Notoginsengstring[] Tempa = Identitycode.split ("|"); - int[] A =New int[18]; the for(inti = 0; i < tempa.length-2; i++) { +A[i] = integer.parseint (tempa[i + 1]); A } the int[] W = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};//weighting Factor + intsum = 0; - for(inti = 0; I < 17; i++) { $sum = sum + a[i] *W[i]; $ } - //Y = mod (S, one) - //get the corresponding check code by modulo the //y:0 1 2 3 4 5 6 7 8 9 - //Check code: 1 0 X 9 8 7 6 5 4 3 2WuyiString[] v = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};//Check Code the inty = sum% 11; - if(!v[y].equalsignorecase (Identitycode.substring (17))) {//18th bit Check code error Wu return false; - } About return true; $ } - - - Public Static voidMain (string[] args) { ASystem.out.println (Checkidentitycode ("110110198001019719")); + } the}
I hope we can provide convenience and help for you.
Java Identity card format strong check (quasi)