去掉了地区验证 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(" "); } } } }