Java ID number verification and 15-bit conversion to 18-bit

Source: Internet
Author: User

Package test; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; /** * ID Card number Verification */public class Identificationcodeutil {public static final int identitycode_old = 15;//old ID card 15-bit public static final int identitycode_new = 18; New ID 18-bit public static int[] Wi = int[17]; /** * To determine whether the ID card number is correct. * @param code * ID number. * @return Returns True if the identity card number is correct, otherwise returns false. */public static Boolean Isidentitycode (String code) {if (Stringutil.empty (code)) {return false;} String birthday = ""; Code = Code.trim (); Only 15 and 182 cases in length if ((Code.length ()!= identitycode_old) && (Code.length ()!= identitycode_new)) {return false;} ID number must be number (18-digit new identity card last one can be x) pattern pt = Pattern.compile ("//d{15,17}" ([//dxx]{1})?); Matcher MT = Pt.matcher (code); if (!mt.find ()) {return false;}//Verify Birthday if (code.length () = = Identitycode_old) {birthday = "" + code.substring (6, 12) ; else {birthday = code.substring (6);} if (! Timeutil.isrightdate (Birthday, "YyyyMMdd")) {return false;}//LastBit check code verification if (code.length () = = identitycode_new) {String lastnum = Getcheckflag (code.substring (0, identitycode_new-1)); Check last digit if (! ("" + Code.charat (identitycode_new-1)). toUpperCase (). Equals (Lastnum)) {return false;}} return true; /** * To obtain the last of the new ID card: Test bit * * @param code * 18 ID of the first 17 * @return The last one of the new ID card/private static string Getcheckflag (string cod e) {int[] vararray = new int[code.length ()]; String lastnum = ""; int numsum = 0; Initialization weight value setwibuffer (); for (int i = 0; i < code.length (); i++) {Vararray[i] = Numberutil.toint ("" + Code.charat (i)); Vararray[i] = vararray[i] * Wi[i]; Numsum = Numsum + vararray[i]; int checkdigit = 12-numsum% 11; Switch (checkdigit) {case 10:lastnum = ' X '; break, case 11:lastnum = ' 0 '; break, case 12:lastnum = ' 1 '; break; default : Lastnum = string.valueof (checkdigit); return lastnum; }/** * Initialization bit value/private static void Setwibuffer () {for (int i = 0; i < wi.length; i++) {int k = (int) Math.pow (2, Wi.length-i)); Wi[i] = k% 11; }/** * Discriminant whether the string is null or has no content, or all spaces. */public static Boolean empty (String o) {return (NULL = O) | | (O.length () <= 0) | | (O.trim (). Equals (""))); /** * Upgrade 15 ID card number to 18 ID card number * * @param code * 15 ID Card number * @return 18-digit ID number/public static string Update2eighteen (String Co DE) {if (Stringutil.empty (code)) {return "";} Code = Code.trim (); if (Code.length ()!= Identitycode_old | |!isidentitycode (code)) {return "";} Code = code.substring (0, 6) + "n" + code.substring (6); Code = code + GETCHECKFLAG (code); return code; public static void Main (string[] args) {string[] Codearray = new string[]{"330521197411044030", "53010119810602007x", " 53010119810602001x "}; for (int i= 0;i<codearray.length;i++) {if (Isidentitycode (codearray[i))) {System.out.println (codearray[i]+) : For the correct ID number. "); } else{System.out.println (codearray[i]+): For the wrong ID number. "); } } SYSTEM.OUT.PRINTLN ("Converted ID number is:" +update2eighteen ("330521820721052")); } }

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.