import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
public class IDCardUtil {
public final static Pattern NUMBERS = Pattern.compile("\\d+");
private static final int CHINA_ID_MIN_LENGTH = 15;
private static final int CHINA_ID_MAX_LENGTH = 18;
public static Exception isValidatedAllIdcard(String idcard) throws Exception {
boolean ret = isIdcard(idcard);
if (!ret) {
throw new Exception("身份证格式有误");
}
return null;
}
final static Map<Integer, String> zoneNum = new HashMap<>();
static {
zoneNum.put(11, "北京");
zoneNum.put(12, "天津");
zoneNum.put(13, "河北");
zoneNum.put(14, "山西");
zoneNum.put(15, "内蒙古");
zoneNum.put(21, "辽宁");
zoneNum.put(22, "吉林");
zoneNum.put(23, "黑龙江");
zoneNum.put(31, "上海");
zoneNum.put(32, "江苏");
zoneNum.put(33, "浙江");
zoneNum.put(34, "安徽");
zoneNum.put(35, "福建");
zoneNum.put(36, "江西");
zoneNum.put(37, "山东");
zoneNum.put(41, "河南");
zoneNum.put(42, "湖北");
zoneNum.put(43, "湖南");
zoneNum.put(44, "广东");
zoneNum.put(45, "广西");
zoneNum.put(46, "海南");
zoneNum.put(50, "重庆");
zoneNum.put(51, "四川");
zoneNum.put(52, "贵州");
zoneNum.put(53, "云南");
zoneNum.put(54, "西藏");
zoneNum.put(61, "陕西");
zoneNum.put(62, "甘肃");
zoneNum.put(63, "青海");
zoneNum.put(64, "宁夏");
zoneNum.put(65, "新疆");
zoneNum.put(71, "台湾");
zoneNum.put(81, "香港");
zoneNum.put(82, "澳门");
zoneNum.put(91, "国外");
}
final static int[] PARITYBIT = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
final static int[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
public static boolean isIdcard(String idCard) {
if (idCard == null || (idCard.length() != 15 && idCard.length() != 18)) {
return false;
}
if (!zoneNum.containsKey(Integer.valueOf(idCard.substring(0, 2)))) {
return false;
}
String year = idCard.length() == 15 ? "19" + idCard.substring(6, 8) : idCard.substring(6, 10);
final int iyear = Integer.parseInt(year);
if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR)) {
return false;
}
String month = idCard.length() == 15 ? idCard.substring(8, 10) : idCard.substring(10, 12);
final int imonth = Integer.parseInt(month);
if (imonth < 1 || imonth > 12) {
return false;
}
String day = idCard.length() == 15 ? idCard.substring(10, 12) : idCard.substring(12, 14);
final int iday = Integer.parseInt(day);
if (iday < 1 || iday > 31) {
return false;
}
if (!isValidDate(year + month + day)) {
return false;
}
int power = 0;
final char[] cs = idCard.toUpperCase().toCharArray();
for (int i = 0; i < cs.length; i++) {
if (i == cs.length - 1 && cs[i] == 'X') {
break;
}
if (cs[i] < '0' || cs[i] > '9') {
return false;
}
if (i < cs.length - 1) {
power += (cs[i] - '0') * POWER_LIST[i];
}
}
if (idCard.length() == 15) {
return true;
}
return cs[cs.length - 1] == PARITYBIT[power % 11];
}
public static boolean isValidDate(String inDate) {
if (inDate == null) {
return false;
}
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMdd");
if (inDate.trim().length() != dataFormat.toPattern().length()) {
return false;
}
dataFormat.setLenient(false);
try {
dataFormat.parse(inDate.trim());
} catch (ParseException e) {
return false;
}
return true;
}
private static Date toBirthDay(String birthday){
try{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(birthday.substring(0, 4)));
calendar.set(Calendar.MONTH, Integer.parseInt(birthday.substring(4, 6)) - 1);
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(birthday.substring(6, 8)));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}catch (Exception e){
return null;
}
}
private static boolean isMatch(Pattern pattern, CharSequence content) {
if (content == null || pattern == null) {
return false;
}
return pattern.matcher(content).matches();
}
private static Date strToDate(final String str, String dateFormat) {
if (str == null || str.trim().length() == 0) {
return null;
}
try {
if (dateFormat == null || dateFormat.length() == 0) {
dateFormat = "yyyy-MM-dd HH:mm:ss";
}
DateFormat fmt = new SimpleDateFormat(dateFormat);
return fmt.parse(str.trim());
} catch (Exception ex) {
return null;
}
}
public static int year(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
return ca.get(Calendar.YEAR);
}
private static char getCheckCode18(int iSum) {
switch (iSum % 11) {
case 10:
return '2';
case 9:
return '3';
case 8:
return '4';
case 7:
return '5';
case 6:
return '6';
case 5:
return '7';
case 4:
return '8';
case 3:
return '9';
case 2:
return 'x';
case 1:
return '0';
case 0:
return '1';
default:
return ' ';
}
}
private static char getCheckCode18(String code17) {
int sum = getPowerSum(code17.toCharArray());
return getCheckCode18(sum);
}
private static int getPowerSum(char[] iArr) {
int iSum = 0;
if (POWER_LIST.length == iArr.length) {
for (int i = 0; i < iArr.length; i++) {
iSum += Integer.valueOf(String.valueOf(iArr[i])) * POWER_LIST[i];
}
}
return iSum;
}
public static String convertIdCard(String idCard) {
StringBuilder idCard18;
if (idCard.length() != CHINA_ID_MIN_LENGTH) {
return null;
}
if (isMatch(NUMBERS, idCard)) {
String birthday = idCard.substring(6, 12);
Date birthDate = strToDate(birthday, "yyMMdd");
int sYear = year(birthDate);
if (sYear > 2000) {
sYear -= 100;
}
idCard18 = new StringBuilder().append(idCard, 0, 6).append(sYear).append(idCard.substring(8));
char sVal = getCheckCode18(idCard18.toString());
idCard18.append(sVal);
} else {
return null;
}
return idCard18.toString();
}
public static Date getBirthDay(String idno){
if(!isIdcard(idno)){
return null;
}
if (idno.length() == 15) {
idno = convertIdCard(idno);
}
return toBirthDay(idno.substring(6, 14));
}
public static String getBirthDayStr(String idno){
if(!isIdcard(idno)){
return null;
}
if (idno.length() == 15) {
idno = convertIdCard(idno);
}
Date birthday = toBirthDay(idno.substring(6, 14));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(birthday);
}
public static String getGender(String idno){
if(!isIdcard(idno)){
return "-1";
}
if (idno.length() == 15) {
idno = convertIdCard(idno);
}
return (Integer.parseInt(idno.substring(16, 17)) % 2) == 0 ? "1" : "0";
}
public static void main(String[] args) {
String idc= "xxx";
boolean idcard = isIdcard(idc);
if (idcard) {
System.out.println("身份证号码合规");
Date birthDay = getBirthDay(idc);
System.out.println("当前身份证的生日为:"+ getBirthDayStr(idc));
String gender = getGender(idc);
if ("0".equals(gender)) {
System.out.println("当前身份证的性别为:男性");
} else if ("1".equals(gender)) {
System.out.println("当前身份证的性别为:女性");
} else {
System.out.println("当前身份证格式不正确");
}
}else {
System.out.println("身份证格式有误");
}
}
}
`
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2020-10-10 【JVM】落日黄昏后:CMS垃圾回收器
2020-10-10 【JVM】如何选择垃圾回收器?