身份證校驗,真實版,遮蔽了地區校驗
阿新 • • 發佈:2018-11-09
去掉了地區驗證 package packer; import java.util.Calendar; public class IdNumberValidate { public String idValidate( String idNumber) { String result="非法身份證"; if (!checkLength(idNumber)) { return result; } if (!checkIllegalChar(idNumber)) { return result; } /* if (!checkRegion(idNumber, refData)) { return result; }*/ if (!checkBirthday(idNumber)) { return result; } if (!checkParityBit(idNumber)) { return result; } return "標準身份證"; } static boolean checkLength(String idNumber) { return idNumber.length() == 15 || idNumber.length() == 18; } static boolean checkIllegalChar(String idNumber) { if (idNumber.matches("[0-9]{15}") || idNumber.matches("[0-9]{17}[0-9|X]")) { return true; } return false; } /*static boolean checkRegion(String idNumber, IdNumberRefData refData) { String region = idNumber.substring(0, 6); Map<String, String> regions = refData.getRegions(); return regions.get(region) != null; }*/ static boolean checkBirthday(String idNumber) { Calendar calendar = Calendar.getInstance(); int currentYear = calendar.get(Calendar.YEAR); int currentMonth = calendar.get(Calendar.MONTH)+1; int currentDay= calendar.get(Calendar.DAY_OF_MONTH); int year; int month; int day; if (idNumber.length() == 15) { year = Integer.parseInt(idNumber.substring(6, 8)) + 1900; month = Integer.parseInt(idNumber.substring(8, 10)); day = Integer.parseInt(idNumber.substring(10, 12)); } else { year = Integer.parseInt(idNumber.substring(6, 10)); month = Integer.parseInt(idNumber.substring(10, 12)); day = Integer.parseInt(idNumber.substring(12, 14)); } if (year < 1900 || year > currentYear) { return false; } if( year == currentYear){ if(month > currentMonth){ return false; }else if(month==currentMonth) { if (day > currentDay) { return false; } } } if (month < 1 || month > 12) { return false; } boolean isLeapYear = year % (year % 100 == 0 ? 400 : 4) == 0; if (day < 1 || day > 31) { return false; } if (month == 2 && day > (isLeapYear ? 29 : 28)) { return false; } if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { return false; } return true; } static boolean checkParityBit(String idNumber) { if (idNumber.length() == 18) { int[] weights = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; int sum = 0; for (int i = 0; i < 17; i++) { sum += weights[i] * (idNumber.charAt(i) - 48); } char[] arr = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}; int remainder = sum % 11; if (idNumber.charAt(17) != arr[remainder]) { return false; } } return true; } }
package packer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Hello { public static void main(String[] agrs) throws IOException { System.out.println( ); System.out.println( ); BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "gbk")); String line = null; while (true) { System.out.println("***輸入身份證號碼查詢此人的年齡狀態,9月以後的預設加一,9月以前的即時滿了一歲,也是減一歲**"); System.out.println("**例如2011年8月31日,由於是9月以前出生的,假如現在是2018年9月一號,實際年齡是7歲,這裡算6歲**"); System.out.println("**例如2011年9月1日,由於是9月以後出生的,假如現在是2018一月一號,實際年齡是6歲,這裡算7歲**"); System.out.println("輸入身份證號碼(輸入exit退出或者ctrl+c):"); line = br.readLine(); if (line.equals("exit")) { break; } IdNumberValidate idNumberValidate=new IdNumberValidate(); idNumberValidate.idValidate(line); if (line.length()<14&&line.length()<19){ System.out.println("請輸入正確的身份證號碼"); System.out.println(" "); }else{ String month = line.substring(10, 12); int result = Integer.parseInt(month); System.out.println(line+"的證件號碼的年齡是:"+BirthdayUntil .getAgeId(line)+",出生月份是:"+result+"月"); System.out.println(line+"的證件號碼的年齡狀態是:"+BirthdayUntil .getAge(BirthdayUntil .getAgeId(line),result)); System.out.println(" "); } } } }