java 身份證工具類
package com.app.wx.common.util;
import org.apache.commons.lang3.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 身份證工具類
*
* <p>
* 先來了解一下身份證的組成規則: <br/>
* 1.號碼的結構 公民身份號碼是特徵組合碼,由十七位數字本體碼和一位校驗碼組成。排列順序從左至右依次為:六位數字地址碼,八位數字出生日期碼,三位數字順序碼和一位數字校驗碼。<br/>
* 2.地址碼 表示編碼物件常住戶口所在縣(市、旗、區)的行政區劃程式碼,按GB/T2260的規定執行。<br/>
* 3.出生日期碼 表示編碼物件出生的年、月、日,按GB/T7408的規定執行,年、月、日程式碼之間不用分隔符。 <br/>
* 4.順序碼 表示在同一地址碼所標識的區域範圍內,對同年、同月、同日出生的人編定的順序號,順序碼的奇數分配給男性,偶數分配給女性。<br/>
* 5.校驗碼 根據前面十七位數字碼,按照ISO 7064:1983.MOD 11-2校驗碼計算出來的檢驗碼。<br/>
* <p>
* @author chen.jh
* @version 1.0, 2018-01-04
*/
public class IdCardUtils extends StringUtils {
/** 中國公民身份證號碼最小長度。 */
public static final int CHINA_ID_MIN_LENGTH = 15;
/** 中國公民身份證號碼最大長度。 */
public static final int CHINA_ID_MAX_LENGTH = 18;
/** 省、直轄市程式碼表 */
public static final String cityCode[] = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37",
"41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91" };
/** 每位加權因子 */
public static final int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
/** 第18位校檢碼 */
public static final String verifyCode[] = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
/** 最低年限 */
public static final int MIN = 1930;
public static Map<String, String> cityCodes = new HashMap<String, String>();
/** 臺灣身份首字母對應數字 */
public static Map<String, Integer> twFirstCode = new HashMap<String, Integer>();
/** 香港身份首字母對應數字 */
public static Map<String, Integer> hkFirstCode = new HashMap<String, Integer>();
static {
cityCodes.put("11", "北京");
cityCodes.put("12", "天津");
cityCodes.put("13", "河北");
cityCodes.put("14", "山西");
cityCodes.put("15", "內蒙古");
cityCodes.put("21", "遼寧");
cityCodes.put("22", "吉林");
cityCodes.put("23", "黑龍江");
cityCodes.put("31", "上海");
cityCodes.put("32", "江蘇");
cityCodes.put("33", "浙江");
cityCodes.put("34", "安徽");
cityCodes.put("35", "福建");
cityCodes.put("36", "江西");
cityCodes.put("37", "山東");
cityCodes.put("41", "河南");
cityCodes.put("42", "湖北");
cityCodes.put("43", "湖南");
cityCodes.put("44", "廣東");
cityCodes.put("45", "廣西");
cityCodes.put("46", "海南");
cityCodes.put("50", "重慶");
cityCodes.put("51", "四川");
cityCodes.put("52", "貴州");
cityCodes.put("53", "雲南");
cityCodes.put("54", "西藏");
cityCodes.put("61", "陝西");
cityCodes.put("62", "甘肅");
cityCodes.put("63", "青海");
cityCodes.put("64", "寧夏");
cityCodes.put("65", "新疆");
cityCodes.put("71", "臺灣");
cityCodes.put("81", "香港");
cityCodes.put("82", "澳門");
cityCodes.put("91", "國外");
twFirstCode.put("A", 10);
twFirstCode.put("B", 11);
twFirstCode.put("C", 12);
twFirstCode.put("D", 13);
twFirstCode.put("E", 14);
twFirstCode.put("F", 15);
twFirstCode.put("G", 16);
twFirstCode.put("H", 17);
twFirstCode.put("J", 18);
twFirstCode.put("K", 19);
twFirstCode.put("L", 20);
twFirstCode.put("M", 21);
twFirstCode.put("N", 22);
twFirstCode.put("P", 23);
twFirstCode.put("Q", 24);
twFirstCode.put("R", 25);
twFirstCode.put("S", 26);
twFirstCode.put("T", 27);
twFirstCode.put("U", 28);
twFirstCode.put("V", 29);
twFirstCode.put("X", 30);
twFirstCode.put("Y", 31);
twFirstCode.put("W", 32);
twFirstCode.put("Z", 33);
twFirstCode.put("I", 34);
twFirstCode.put("O", 35);
hkFirstCode.put("A", 1);
hkFirstCode.put("B", 2);
hkFirstCode.put("C", 3);
hkFirstCode.put("R", 18);
hkFirstCode.put("U", 21);
hkFirstCode.put("Z", 26);
hkFirstCode.put("X", 24);
hkFirstCode.put("W", 23);
hkFirstCode.put("O", 15);
hkFirstCode.put("N", 14);
}
/**
* 將15位身份證號碼轉換為18位
* @param idCard 15位身份編碼
* @return 18位身份編碼
*/
public static String conver15CardTo18(String idCard) {
String idCard18 = "";
if (idCard.length() != CHINA_ID_MIN_LENGTH) {
return null;
}
if (isNum(idCard)) {
// 獲取出生年月日
String birthday = idCard.substring(6, 12);
Date birthDate = null;
try {
birthDate = new SimpleDateFormat("yyMMdd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (birthDate != null)
cal.setTime(birthDate);
// 獲取出生年(完全表現形式,如:2010)
String sYear = String.valueOf(cal.get(Calendar.YEAR));
idCard18 = idCard.substring(0, 6) + sYear + idCard.substring(8);
// 轉換字元陣列
char[] cArr = idCard18.toCharArray();
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
int iSum17 = getPowerSum(iCard);
// 獲取校驗位
String sVal = getCheckCode18(iSum17);
if (sVal.length() > 0) {
idCard18 += sVal;
} else {
return null;
}
}
} else {
return null;
}
return idCard18;
}
/**
* 根據正則校驗18位身份證
* @param idCard 身份編碼
* @return
*/
public static boolean validateIDCardByRegex(String idCard) {
String curYear = "" + Calendar.getInstance().get(Calendar.YEAR);
int y3 = Integer.valueOf(curYear.substring(2, 3));
int y4 = Integer.valueOf(curYear.substring(3, 4));
// 43 0103 1973 0 9 30 051 9
return idCard.matches("^(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|71|8[1-2])\\d{4}(19\\d{2}|20([0-" + (y3 - 1) + "][0-9]|" + y3
+ "[0-" + y4 + "]))(((0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])))\\d{3}([0-9]|x|X)$");
// 44 1825 1988 0 7 1 3 003 4
}
/**
* 驗證身份證是否合法
* @param idCard
* @return
*/
public static boolean validateIdCard(String idCard) {
String card = idCard.trim();
if (validateIdCard18(card)) {
return true;
}
if (validateIdCard15(card)) {
return true;
}
String[] cardval = validateIdCard10(card);
if (cardval != null) {
if (cardval[2].equals("true")) {
return true;
}
}
return false;
}
/**
* 驗證大陸身份證是否合法
* @param idCard
* @return
*/
public static boolean validateMainLandIdCard(String idCard) {
String card = idCard.trim();
if (validateIdCard18(card)) {
return true;
}
if (validateIdCard15(card)) {
return true;
}
return false;
}
/**
* 驗證18位身份編碼是否合法
* @param idCard 身份編碼
* @return 是否合法
*/
public static boolean validateIdCard18(String idCard) {
boolean bTrue = false;
if (idCard.length() == CHINA_ID_MAX_LENGTH) {
// 前17位
String code17 = idCard.substring(0, 17);
// 第18位
String code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH);
if (isNum(code17)) {
char[] cArr = code17.toCharArray();
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
int iSum17 = getPowerSum(iCard);
// 獲取校驗位
String val = getCheckCode18(iSum17);
if (val.length() > 0) {
if (val.equalsIgnoreCase(code18)) {
bTrue = true;
}
}
}
}
}
// 校驗通過 且 正則校驗通過 且 可以獲取到省份資訊
return bTrue && validateIDCardByRegex(idCard) && getProvinceByIdCard(idCard) != null;
}
/**
* 驗證15位身份編碼是否合法
* @param idCard 身份編碼
* @return 是否合法
*/
public static boolean validateIdCard15(String idCard) {
if (idCard.length() != CHINA_ID_MIN_LENGTH) {
return false;
}
if (isNum(idCard)) {
String proCode = idCard.substring(0, 2);
if (cityCodes.get(proCode) == null) {
return false;
}
String birthCode = idCard.substring(6, 12);
Date birthDate = null;
try {
birthDate = new SimpleDateFormat("yy").parse(birthCode.substring(0, 2));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (birthDate != null)
cal.setTime(birthDate);
if (!valiDate(cal.get(Calendar.YEAR), Integer.valueOf(birthCode.substring(2, 4)), Integer.valueOf(birthCode.substring(4, 6)))) {
return false;
}
} else {
return false;
}
return true;
}
/**
* 驗證10位身份編碼是否合法
* @param idCard 身份編碼
* @return 身份證資訊陣列
* <p>
* [0] - 臺灣、澳門、香港 [1] - 性別(男M,女F,未知N) [2] - 是否合法(合法true,不合法false) 若不是身份證件號碼則返回null
* </p>
*/
public static String[] validateIdCard10(String idCard) {
String[] info = new String[3];
String card = idCard.replaceAll("[\\(|\\)]", "");
if (card.length() != 8 && card.length() != 9 && idCard.length() != 10) {
return null;
}
if (idCard.matches("^[a-zA-Z][0-9]{9}$")) { // 臺灣
info[0] = "臺灣";
System.out.println("11111");
String char2 = idCard.substring(1, 2);
if (char2.equals("1")) {
info[1] = "M";
System.out.println("MMMMMMM");
} else if (char2.equals("2")) {
info[1] = "F";
System.out.println("FFFFFFF");
} else {
info[1] = "N";
info[2] = "false";
System.out.println("NNNN");
return info;
}
info[2] = validateTWCard(idCard) ? "true" : "false";
} else if (idCard.matches("^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$")) { // 澳門
info[0] = "澳門";
info[1] = "N";
} else if (idCard.matches("^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$")) { // 香港
info[0] = "香港";
info[1] = "N";
info[2] = validateHKCard(idCard) ? "true" : "false";
} else {
return null;
}
return info;
}
/**
* 驗證臺灣身份證號碼
* @param idCard 身份證號碼
* @return 驗證碼是否符合
*/
public static boolean validateTWCard(String idCard) {
String start = idCard.substring(0, 1);
String mid = idCard.substring(1, 9);
String end = idCard.substring(9, 10);
Integer iStart = twFirstCode.get(start);
Integer sum = iStart / 10 + (iStart % 10) * 9;
char[] chars = mid.toCharArray();
Integer iflag = 8;
for (char c : chars) {
sum = sum + Integer.valueOf(c + "") * iflag;
iflag--;
}
return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.valueOf(end) ? true : false;
}
/**
* 驗證香港身份證號碼(存在Bug,部份特殊身份證無法檢查)
* <p>
* 身份證前2位為英文字元,如果只出現一個英文字元則表示第一位是空格,對應數字58 前2位英文字元A-Z分別對應數字10-35 最後一位校驗碼為0-9的數字加上字元"A","A"代表10
* </p>
* <p>
* 將身份證號碼全部轉換為數字,分別對應乘9-1相加的總和,整除11則證件號碼有效
* </p>
*
* @param idCard 身份證號碼
* @return 驗證碼是否符合
*/
public static boolean validateHKCard(String idCard) {
String card = idCard.replaceAll("[\\(|\\)]", "");
Integer sum = 0;
if (card.length() == 9) {
sum = (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 9
+ (Integer.valueOf(card.substring(1, 2).toUpperCase().toCharArray()[0]) - 55) * 8;
card = card.substring(1, 9);
} else {
sum = 522 + (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 8;
}
String mid = card.substring(1, 7);
String end = card.substring(7, 8);
char[] chars = mid.toCharArray();
Integer iflag = 7;
for (char c : chars) {
sum = sum + Integer.valueOf(c + "") * iflag;
iflag--;
}
if (end.toUpperCase().equals("A")) {
sum = sum + 10;
} else {
sum = sum + Integer.valueOf(end);
}
return (sum % 11 == 0) ? true : false;
}
/**
* 將字元陣列轉換成數字陣列
* @param ca 字元陣列
* @return 數字陣列
*/
public static int[] converCharToInt(char[] ca) {
int len = ca.length;
int[] iArr = new int[len];
try {
for (int i = 0; i < len; i++) {
iArr[i] = Integer.parseInt(String.valueOf(ca[i]));
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return iArr;
}
/**
* 將身份證的每位和對應位的加權因子相乘之後,再得到和值
* @param iArr
* @return 身份證編碼。
*/
public static int getPowerSum(int[] iArr) {
int iSum = 0;
if (power.length == iArr.length) {
for (int i = 0; i < iArr.length; i++) {
for (int j = 0; j < power.length; j++) {
if (i == j) {
iSum = iSum + iArr[i] * power[j];
}
}
}
}
return iSum;
}
/**
* 將power和值與11取模獲得餘數進行校驗碼判斷
* @param iSum
* @return 校驗位
*/
public static String getCheckCode18(int iSum) {
String sCode = "";
switch (iSum % 11) {
case 10:
sCode = "2";
break;
case 9:
sCode = "3";
break;
case 8:
sCode = "4";
break;
case 7:
sCode = "5";
break;
case 6:
sCode = "6";
break;
case 5:
sCode = "7";
break;
case 4:
sCode = "8";
break;
case 3:
sCode = "9";
break;
case 2:
sCode = "x";
break;
case 1:
sCode = "0";
break;
case 0:
sCode = "1";
break;
}
return sCode;
}
/**
* 根據身份編號獲取年齡
* @param idCard 身份編號
* @return 年齡
*/
public static int getAgeByIdCard(String idCard) {
int iAge = 0;
if (idCard.length() == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
String year = idCard.substring(6, 10);
Calendar cal = Calendar.getInstance();
int iCurrYear = cal.get(Calendar.YEAR);
iAge = iCurrYear - Integer.valueOf(year);
return iAge;
}
/**
* 根據身份編號獲取生日
* @param idCard 身份編號
* @return 生日(yyyyMMdd)
*/
public static String getBirthByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return idCard.substring(6, 14);
}
/**
* 根據身份編號獲取生日
* @param idCard 身份編號
* @return 生日(yyyy-MM-dd)
*/
public static String getBirthByIdCard2(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
}
/**
* 根據身份編號獲取生日年
* @param idCard 身份編號
* @return 生日(yyyy)
*/
public static Short getYearByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return Short.valueOf(idCard.substring(6, 10));
}
/**
* 根據身份編號獲取生日月
* @param idCard 身份編號
* @return 生日(MM)
*/
public static Short getMonthByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return Short.valueOf(idCard.substring(10, 12));
}
/**
* 根據身份編號獲取生日天
* @param idCard 身份編號
* @return 生日(dd)
*/
public static Short getDateByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return Short.valueOf(idCard.substring(12, 14));
}
/**
* 根據身份編號獲取性別
* @param idCard 身份編號
* @return 性別(M-男,F-女,N-未知)
*/
public static String getGenderByIdCard(String idCard) {
String sGender = "N";
if (idCard.length() == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
String sCardNum = idCard.substring(16, 17);
if (Integer.parseInt(sCardNum) % 2 != 0) {
sGender = "M";
} else {
sGender = "F";
}
return sGender;
}
/**
* 根據身份編號獲取戶籍省份
* @param idCard 身份編碼
* @return 省級編碼。
*/
public static String getProvinceByIdCard(String idCard) {
int len = idCard.length();
String sProvince = null;
String sProvinNum = "";
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
sProvinNum = idCard.substring(0, 2);
}
sProvince = cityCodes.get(sProvinNum);
return sProvince;
}
/**
* 數字驗證
* @param val
* @return 提取的數字。
*/
public static boolean isNum(String val) {
return val == null || "".equals(val) ? false : val.matches("^[0-9]*$");
}
/**
* 驗證小於當前日期 是否有效
* @param iYear 待驗證日期(年)
* @param iMonth 待驗證日期(月 1-12)
* @param iDate 待驗證日期(日)
* @return 是否有效
*/
public static boolean valiDate(int iYear, int iMonth, int iDate) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int datePerMonth;
if (iYear < MIN || iYear >= year) {
return false;
}
if (iMonth < 1 || iMonth > 12) {
return false;
}
switch (iMonth) {
case 4:
case 6:
case 9:
case 11:
datePerMonth = 30;
break;
case 2:
boolean dm = ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0)) && (iYear > MIN && iYear < year);
datePerMonth = dm ? 29 : 28;
break;
default:
datePerMonth = 31;
}
return (iDate >= 1) && (iDate <= datePerMonth);
}
/**
* 根據身份證號,自動獲取對應的星座
* @param idCard 身份證號碼
* @return 星座
*/
public static String getConstellationById(String idCard) {
if (!validateIdCard(idCard)) {
return "";
}
int month = getMonthByIdCard(idCard);
int day = getDateByIdCard(idCard);
String strValue = "";
if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
strValue = "水瓶座";
} else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
strValue = "雙魚座";
} else if ((month == 3 && day > 20) || (month == 4 && day <= 19)) {
strValue = "白羊座";
} else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
strValue = "金牛座";
} else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
strValue = "雙子座";
} else if ((month == 6 && day > 21) || (month == 7 && day <= 22)) {
strValue = "巨蟹座";
} else if ((month == 7 && day > 22) || (month == 8 && day <= 22)) {
strValue = "獅子座";
} else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
strValue = "處女座";
} else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
strValue = "天秤座";
} else if ((month == 10 && day > 23) || (month == 11 && day <= 22)) {
strValue = "天蠍座";
} else if ((month == 11 && day > 22) || (month == 12 && day <= 21)) {
strValue = "射手座";
} else if ((month == 12 && day > 21) || (month == 1 && day <= 19)) {
strValue = "魔羯座";
}
return strValue;
}
/**
* 根據身份證號,自動獲取對應的生肖
* @param idCard 身份證號碼
* @return 生肖
*/
public static String getZodiacById(String idCard) {
if (!validateIdCard(idCard)) {
return "";
}
String sSX[] = { "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗" };
int year = getYearByIdCard(idCard);
int end = 3;
int x = (year - end) % 12;
String retValue = "";
retValue = sSX[x];
return retValue;
}
/**
* 根據身份證號,自動獲取對應的天干地支
* @param idCard 身份證號碼
* @return 天干地支
*/
public static String getChineseEraById(String idCard) {
if (!validateIdCard(idCard)) {
return "";
}
String sTG[] = { "癸", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "任" };
String sDZ[] = { "亥", "子", "醜", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌" };
int year = getYearByIdCard(idCard);
int i = (year - 3) % 10;
int j = (year - 3) % 12;
String retValue = "";
retValue = sTG[i] + sDZ[j];
return retValue;
}
public static void main(String[] args) {
String idCard = "450923198910028012";
System.out.println("驗證大陸身份證:" + validateIdCard(idCard));
System.out.println("驗證香港身份證:" + validateHKCard("Z2673259"));
System.out.println("獲取性別:" + getGenderByIdCard(idCard));
System.out.println("獲取年齡:" + getAgeByIdCard(idCard));
System.out.println("獲取生日(yyyyMMdd):" + getBirthByIdCard(idCard));
System.out.println("身份編號(yyyy-MM-dd):" + getBirthByIdCard2(idCard));
System.out.println("獲取生日月:" + getMonthByIdCard(idCard));
System.out.println("獲取生日天:" + getDateByIdCard(idCard));
System.out.println("獲取對應的星座:" + getConstellationById(idCard));
System.out.println("獲取對應的生肖:" + getZodiacById(idCard));
System.out.println("獲取對應的天干地支:" + getChineseEraById(idCard));
System.out.println("獲取戶籍省份:" + getProvinceByIdCard(idCard));
}
}