package com.cloud.economics.common.utils; import java.text.SimpleDateFormat; import java.util.Date; /** * 验证身份证号码 身份证号码, 可以解析身份证号码的各个字段, * 以及验证身份证号码是否有效; 身份证号码构成:6位地址编码+8位生日+3位顺序码+1位校验码 * * @ClassName: CheckIdCard * @Description: TODO * @author fdx * */ public class CheckIdCard{ private final static String BIRTH_DATE_FORMAT = "yyyyMMdd"; // 身份证号码中的出生日期的格式 private final static Date MINIMAL_BIRTH_DATE = new Date(-2209017600000L); // 身份证的最小出生日期,1900年1月1日 private final static int NEW_CARD_NUMBER_LENGTH = 18; private final static int OLD_CARD_NUMBER_LENGTH = 15; length()){ cardNumber = contertToNewCardNumber(cardNumber); } return validate(cardNumber); } return false; } public static boolean validate(String cardNumber){ boolean result = true; result = result && (null != cardNumber); // ID number cannot be empty result = result && NEW_CARD_NUMBER_LENGTH == cardNumber. length(); // The length of the ID number is 18 (new card) // The first 17 digits of the ID number must be Arabic numerals for (int i = 0; result && i < NEW_CARD_NUMBER_LENGTH - 1; i++){ char ch = cardNumber.charAt(i); result = result && ch >= '0' && ch <= '9'; } // The first of the ID number The 8-digit checksum is correct result = result && (calculateVerifyCode(cardNumber) == cardNumber .charAt(NEW_CARD_NUMBER_LENGTH - 1)); // The date of birth cannot be later than the current time and cannot be earlier than 1900 try{ Date birthDate = new SimpleDateFormat(BIRTH_DATE_FORMAT) .parse(getBirthDayPart(cardNumber)); result = result && null != birthDate; result = result && birthDate.before(new Date()); result = result && birthDate.after(MINIMAL_BIRTH_DATE); /** * The year, month, and day in the date of birth must be correct, for example, the month range is [1,12], * the date range is [1,31], and when leap year, big month, and small month need to be checked, * the month and date match * / String birthdayPart = getBirthDayPart(cardNumber); realBirthdayPart = new SimpleDateFormat(BIRTH_DATE_FORMAT) .format(birthDate); result = result && (birthdayPart.equals(realBirthdayPart)); } catch(Exception e){ result = false; } return result; } public static String getBirthDayPart(String cardNumber){ return cardNumber.substring(6, 14 ) ; number of digits): * * Seventeen-digit body code weighted sum formula S = Sum(Ai * Wi), i = 0...16, first sum the weights of the first 17 digits; * 2; Calculation modulo Y = mod(S, 11)< Obtain the corresponding check code Y through the modulus: 0 1 2 3 4 5 6 7 8 9 10 Check code: 1 0 X 9 * 8 7 6 5 4 3 2 * * @param cardNumber * @return * / 4 charAt(i); sum += ((int) (ch - '0')) * VERIFY_CODE_WEIGHT[i]; } return VERIFY_CODE[sum % 11]; } /** * Convert 15-digit ID number to 18-digit ID number<br> * The difference between 15-digit ID number and 18-digit ID number is:<br> * 1. In the 15-digit ID number, "year of birth" The field is 2 digits, and "19" needs to be added during conversion, indicating the 20th century<br> * 2. There is no check code for the last digit of the 15-digit ID card . In the 18-digit ID card, the verification code is generated according to the first 17 digits * * @param cardNumber * @return */ private static String contertToNewCardNumber(String oldCardNumber){ StringBuilder buf = new StringBuilder(NEW_CARD_NUMBER_LENGTH); buf.append(oldCardNumber.substring(0, 6)); buf.append("19"); buf.append(oldCardNumber.substring(6)); buf.append(CheckIdCard.calculateVerifyCode(buf)); return buf.toString(); } }
Tool class for verifying ID number
Guess you like
Origin blog.csdn.net/FDX0821/article/details/123544575
Recommended
Ranking