package com.souche.vs.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author
*/
public class IDCardUtils {
private static Pattern NUMBER_PATTERN = Pattern.compile("[0-9]*");
private static Pattern IDCARD_NUMBER_PATTERN = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
/** 检查护照是否合法 */
public static final String PASSPORT1 = "^[a-zA-Z]{5,17}$";
public static final String PASSPORT2 = "^[a-zA-Z0-9]{5,17}$";
public static boolean validateIDCard2(String idStr){
if (PASSPORT1.matches(idStr) || PASSPORT2.matches(idStr)){
return true;
}
return false;
}
/**
* 功能:身份证的有效验证
*
* @param idStr 身份证号
* @return 有效:返回"true" ;无效:返回String信息
*/
public static String validateIDCard(String idStr) {
// 记录错误信息
String errorInfo = "true";
String[] valCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"};
String[] wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
String ai = "";
String ai1;
String ai2;
// 号码的长度 15位或18位
if (idStr.length() != 15 && idStr.length() != 18) {
return "身份证号格式不正确";
}
//数字 除最后一位都为数字
if (idStr.length() == 18) {
ai = idStr.substring(0, 17);
} else if (idStr.length() == 15) {
ai = idStr.substring(0, 6) + "19" + idStr.substring(6, 15);
}
if (!isNumeric(ai)) {
errorInfo = "身份证号格式不正确";
return errorInfo;
}
// 出生年月是否有效
// 年份
String strYear = ai.substring(6, 10);
// 月份
String strMonth = ai.substring(10, 12);
// 天
String strDay = ai.substring(12, 14);
if (!isDataFormat(strYear + "-" + strMonth + "-" + strDay)) {
errorInfo = "身份证生日无效。";
return errorInfo;
}
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
try {
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
|| (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
errorInfo = "身份证生日不在有效范围。";
return errorInfo;
}
} catch (NumberFormatException | ParseException e) {
e.printStackTrace();
}
if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
errorInfo = "身份证月份无效";
return errorInfo;
}
if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
errorInfo = "身份证日期无效";
return errorInfo;
}
//地区码时候有效
Map<String, String> h = getAreaCode();
if (h.get(ai.substring(0, 2)) == null) {
errorInfo = "身份证地区编码错误";
return errorInfo;
}
//判断最后一位的值
int totalmulAiWi = 0;
for (int i = 0; i < 17; i++) {
totalmulAiWi = totalmulAiWi
+ Integer.parseInt(String.valueOf(ai.charAt(i)))
* Integer.parseInt(wi[i]);
}
int modValue = totalmulAiWi % 11;
String strVerifyCode = valCodeArr[modValue];
ai1 = ai + strVerifyCode.toUpperCase();
ai2 = ai + strVerifyCode.toLowerCase();
if (idStr.length() == 18) {
if (!ai1.equals(idStr) && !ai2.equals(idStr)) {
errorInfo = "身份证号格式不正确";
return errorInfo;
}
} else {
return errorInfo;
}
return errorInfo;
}
/**
* 功能:设置地区编码
*
* @return Hashtable 对象
*/
private static Map<String,String> getAreaCode() {
Map<String, String> hashTable = new Hashtable<>();
hashTable.put("11", "北京");
hashTable.put("12", "天津");
hashTable.put("13", "河北");
hashTable.put("14", "山西");
hashTable.put("15", "内蒙古");
hashTable.put("21", "辽宁");
hashTable.put("22", "吉林");
hashTable.put("23", "黑龙江");
hashTable.put("31", "上海");
hashTable.put("32", "江苏");
hashTable.put("33", "浙江");
hashTable.put("34", "安徽");
hashTable.put("35", "福建");
hashTable.put("36", "江西");
hashTable.put("37", "山东");
hashTable.put("41", "河南");
hashTable.put("42", "湖北");
hashTable.put("43", "湖南");
hashTable.put("44", "广东");
hashTable.put("45", "广西");
hashTable.put("46", "海南");
hashTable.put("50", "重庆");
hashTable.put("51", "四川");
hashTable.put("52", "贵州");
hashTable.put("53", "云南");
hashTable.put("54", "西藏");
hashTable.put("61", "陕西");
hashTable.put("62", "甘肃");
hashTable.put("63", "青海");
hashTable.put("64", "宁夏");
hashTable.put("65", "新疆");
hashTable.put("71", "台湾");
hashTable.put("81", "香港");
hashTable.put("82", "澳门");
hashTable.put("91", "国外");
return hashTable;
}
/**
* 验证日期字符串是否是YYYY-MM-DD格式
*
* @param str str
* @return boolean
*/
private static boolean isDataFormat(String str) {
Matcher isNo = IDCARD_NUMBER_PATTERN.matcher(str);
return isNo.matches();
}
/**
* 功能:判断字符串是否为数字
*
* @param str str
* @return boolean
*/
private static boolean isNumeric(String str) {
Matcher isNum = NUMBER_PATTERN.matcher(str);
return isNum.matches();
}
/**
* 方法名:parseGender
* 详述:根据所传身份证号解析其性别
* 开发人员:souvc
* 创建时间:2015-9-7 下午1:55:44
* @return 说明返回值含义
* @throw 说明发生此异常的条件
*/
public static String parseGender(String cid) {
String gender = null;
char c = cid.charAt(cid.length()-2);
int sex = Integer.parseInt(String.valueOf(c));
if(sex%2==0){
gender = "女士";
}else{
gender = "先生";
}
return gender;
}
public static void main(String[] args) {
String validateIDCard = validateIDCard("342201189010191815");
System.out.println(validateIDCard);
if(!"true".equals(validateIDCard)) {
System.out.println(validateIDCard);
}
}
}
Java 身份证校验工具类
最新推荐文章于 2024-06-17 16:15:26 发布