package burp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IDCardValidator {
// 身份证号的正则表达式
private static final String ID_CARD_REGEX = "^(\\d{17}[0-9Xx]|\\d{15})$";
// 加权因子,权重系数
private static final int[] WEIGHT = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
// 校验码映射表
private static final char[] CHECK_CODES = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
/**
* 校验身份证号是否合法,并返回身份证号码信息
* @param idCard 身份证号
* @return 如果合法返回身份证信息,否则返回错误信息
*/
public static String validateAndExtractInfo(String idCard) {
// 校验格式
if (!idCard.matches(ID_CARD_REGEX)) {
return "身份证号格式不正确";
}
// 如果是15位身份证号,则转换为18位
if (idCard.length() == 15) {
idCard = convertTo18(idCard);
}
// 校验出生日期是否合法
if (!isValidDate(idCard.substring(6, 14))) {
return "身份证号中的出生日期无效";
}
// 校验性别是否合法(第17位,奇数为男,偶数为女)
if (!isValidGender(idCard.charAt(16))) {
return "身份证号中的性别信息无效";
}
// 校验校验码是否合法
if (!isValidCheckCode(idCard)) {
return "身份证号中的校验码无效";
}
// 提取信息并返回
String birthDate = idCard.substring(6, 14);
String gender = (idCard.charAt(16) % 2 == 0) ? "女" : "男";
String formattedDate = formatDate(birthDate);
String card= String.format( idCard, formattedDate, gender);
return card;
}
/**
* 将15位身份证号转换为18位
* @param idCard 15位身份证号
* @return 18位身份证号
*/
private static String convertTo18(String idCard) {
StringBuilder sb = new StringBuilder(idCard);
sb.insert(6, "19"); // 插入年份前缀“19”,形成18位
String newIdCard = sb.toString();
char[] c = newIdCard.toCharArray();
int[] ai = new int[18];
// 将字符串转换为数字数组
for (int i = 0; i < 17; i++) {
ai[i] = c[i] - '0';
}
// 计算加权和
int sum = 0;
for (int i = 0; i < 17; i++) {
sum += ai[i] * WEIGHT[i];
}
// 校验码计算
int mod = sum % 11;
char checkCode = CHECK_CODES[mod];
// 返回18位身份证号
return newIdCard + checkCode;
}
/**
* 校验日期是否合法
* @param birthDate 出生日期 (YYYYMMDD)
* @return 如果日期合法返回true,否则返回false
*/
private static boolean isValidDate(String birthDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setLenient(false);
try {
sdf.parse(birthDate);
return true;
} catch (ParseException e) {
return false;
}
}
/**
* 校验性别是否合法
* @param gender 性别标识 (第17位)
* @return 如果性别合法返回true,否则返回false
*/
private static boolean isValidGender(char gender) {
return (gender - '0') % 2 == 1 || gender == 'X' || gender == 'x';
}
/**
* 校验校验码是否有效
* @param idCard 身份证号
* @return 如果校验码合法返回true,否则返回false
*/
private static boolean isValidCheckCode(String idCard) {
char[] idCardChars = idCard.toCharArray();
int sum = 0;
// 计算前17位的加权和
for (int i = 0; i < 17; i++) {
sum += (idCardChars[i] - '0') * WEIGHT[i];
}
// 校验码映射
int mod = sum % 11;
char checkCode = CHECK_CODES[mod];
// 校验第18位是否与计算的校验码匹配
return checkCode == idCardChars[17];
}
/**
* 格式化出生日期为 YYYY-MM-DD
* @param birthDate 出生日期 (YYYYMMDD)
* @return 格式化后的日期字符串
*/
private static String formatDate(String birthDate) {
return birthDate.substring(0, 4) + "-" + birthDate.substring(4, 6) + "-" + birthDate.substring(6, 8);
}
public static void main(String[] args) {
System.out.println(IDCardValidator.validateAndExtractInfo(""));
}
}