java實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的示例代碼
需要編號(hào)文件
編號(hào)文件部分內(nèi)容如下
11:北京市
1101:市轄區(qū)
110101:東城區(qū)
110102:西城區(qū)
110105:朝陽(yáng)區(qū)
110106:豐臺(tái)區(qū)
110107:石景山區(qū)
110108:海淀區(qū)
......
java代碼如下
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Pattern;
public class IdCardCheckUtils {
public static final Integer[] idCardWeight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};//身份證前17位數(shù)字依次乘以對(duì)應(yīng)的權(quán)重因子
public static final String[] CONSTELLATION_ARR = {"水瓶座", "雙魚(yú)座", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "魔羯座"};//星座數(shù)組
public static final int[] CONSTELLATION_EDGE_DAY = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};//星座對(duì)應(yīng)的邊緣日期
public static final String[] ZODIAC_ARR = {"猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊"};//生肖
public static Map<Integer, String> idCardMap = new HashMap<>();//組裝根據(jù)余數(shù),對(duì)應(yīng)一個(gè)指定的校驗(yàn)碼
private static Map<Integer, String> nativePlaceCode = new HashMap<>(4096, 1);//內(nèi)存籍貫編號(hào),記錄身份證編號(hào)對(duì)應(yīng)的地址
public static void main(String[] args) throws Exception {
String path = "C:\\Users\\Administrator\\Desktop\\code.txt";//編號(hào)文件
init(path);//初始化身份證校驗(yàn)參數(shù)
String idCard = "512926164805034455";//測(cè)試的身份證號(hào)碼
checkIdCard(idCard);//校驗(yàn)身份證是否輸入正常
//基本信息
System.out.println("出生日期:" + idCard.substring(6, 10) + "." + idCard.substring(10, 12) + "." + idCard.substring(12, 14));
System.out.println("性別:" +getSex(idCard));
System.out.println("年齡:" + getAge(idCard));
System.out.println("您的星座:" + getConstellation(idCard));
System.out.println("您的生肖:" + getAnimalSign(idCard));
//籍貫信息
int nativePlaceCode = Integer.parseInt(idCard.substring(0, 6));//籍貫組合編碼
int provinceCode = nativePlaceCode / 10000;//省編碼
int cityCode = nativePlaceCode / 100;//市編碼
int countyCode = nativePlaceCode;//縣編碼
System.out.println(IdCardCheckUtils.nativePlaceCode.get(provinceCode));
System.out.println(IdCardCheckUtils.nativePlaceCode.get(cityCode));
System.out.println(IdCardCheckUtils.nativePlaceCode.get(countyCode));
}
/**
* 初始化地區(qū)編碼文件與校驗(yàn)碼
*
* @param path
* @throws IOException
*/
private static void init(String path) throws IOException {
synchronized (String.class) {
if (!idCardMap.isEmpty()) return;
Consumer<String> function = line -> {
String[] split = line.split(":");
nativePlaceCode.put(Integer.valueOf(split[0]), split[1]);
};
read(path, function);//將文件內(nèi)容加載到map內(nèi)存中
final String[] idCardCheck = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};//身份證最后一位對(duì)應(yīng)的校驗(yàn)碼
for (int i = 0; i < 10; i++) {
idCardMap.put(i, idCardCheck[i]);//校驗(yàn)碼記錄
}
}
}
/**
* 驗(yàn)證身份證號(hào)碼是否正常
*
* @param idCardNo
* @return
*/
public static void checkIdCard(String idCardNo) throws Exception {
String idCard = idCardNo.toUpperCase();//將其轉(zhuǎn)成大寫(xiě)有的身份證最后一位是字母
if (idCardNo.length() == 15) {//15位身份證轉(zhuǎn)成18位
if (!(idCardNo.matches("[0-9]{17}[0-9|x]|[0-9]{15}"))) throw new Exception("身份證號(hào)碼輸入錯(cuò)誤,請(qǐng)輸入正確格式的15位身份證號(hào)碼");
String s2 = idCardNo.substring(0, 6);//15位轉(zhuǎn)換為18位
String s3 = idCardNo.substring(6, 15);
String changed = s2.concat("19").concat(s3);
idCard = changed.toUpperCase();
}
if (!Pattern.matches("^\\d{17}", idCard.substring(0, 17))) throw new Exception("身份證號(hào)碼輸入錯(cuò)誤,前17位必須是數(shù)字");//驗(yàn)證身份證前17位是否為數(shù)字
char[] idCardCharNumber = idCard.toCharArray();
Integer resultSum = 0;
for (int i = 0; i < idCardCharNumber.length - 1; i++) resultSum += Character.getNumericValue(idCardCharNumber[i]) * idCardWeight[i];
Integer lastResult = resultSum % 11;//將相加的前17位數(shù)字依次乘以對(duì)應(yīng)的權(quán)重因子相加,相加的結(jié)果除以11,得到余數(shù)
//根據(jù)余數(shù),對(duì)應(yīng)一個(gè)指定的校驗(yàn)碼。最終得到的校驗(yàn)碼就是身份證號(hào)碼的最后一位數(shù)字。通過(guò)這個(gè)校驗(yàn)碼,可以驗(yàn)證前面17位數(shù)字是否正確,從而提高身份證號(hào)碼的準(zhǔn)確性
if (!(idCardMap.containsKey(lastResult)) || !(idCardMap.get(lastResult).equals(idCard.substring(idCard.length() - 1)))) throw new Exception("身份證號(hào)碼校驗(yàn)異常,輸入錯(cuò)誤");
}
/**
* 根據(jù)日期獲取當(dāng)前年齡
*
* @return
*/
public static int getAge(String idCard) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = dateFormat.format(new Date());
int currentDate = Integer.parseInt(dateString);
int idCardDate = Integer.parseInt(idCard.substring(6, 10) + idCard.substring(10, 12) + idCard.substring(12, 14));
int age = (currentDate - idCardDate) / 10000;
return age;
}
/**
* 根據(jù)身份證id獲取當(dāng)前年齡
*
* @return
*/
public static String getSex(String idCard) {
String sex = Integer.parseInt(idCard.substring(16, 17)) % 2 == 0 ? "女" : "男";
return sex;
}
/**
* 根據(jù)身份證號(hào)判斷用戶星座
*
* @param cardNo
* @return
*/
public static String getConstellation(String cardNo) {
String birthday = cardNo.substring(6, 14);// 獲取出生日期
Date birthdate = null;
try {
birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
if (birthdate == null) return "";
Calendar cal = Calendar.getInstance();
cal.setTime(birthdate);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
if (day < CONSTELLATION_EDGE_DAY[month]) month = month - 1;
if (month >= 0) return CONSTELLATION_ARR[month];
return CONSTELLATION_ARR[11];// default to return 魔羯
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 根據(jù)身份證號(hào)判斷用戶生肖
*
* @param cardNo
* @return
*/
public static String getAnimalSign(String cardNo) {
String birthday = cardNo.substring(6, 14);// 獲取出生日期
Date birthdate;
try {
birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
Calendar cal = Calendar.getInstance();
cal.setTime(birthdate);
return ZODIAC_ARR[cal.get(Calendar.YEAR) % 12];
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void read(String path, Consumer<String> func) throws IOException {
File file = new File(path);
FileInputStream fileInputStream = null;
InputStreamReader read = null;//考慮到編碼格式
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
read = new InputStreamReader(fileInputStream, "UTF-8");
bufferedReader = new BufferedReader(read);
String lineTxt;
while ((lineTxt = bufferedReader.readLine()) != null) func.accept(lineTxt);//讀取一行
} catch (IOException e) {
e.printStackTrace();
} finally {
bufferedReader.close();
read.close();
fileInputStream.close();
}
}
}運(yùn)行結(jié)果 ,身份證是隨意編寫(xiě)的,可以用自己的身份證進(jìn)行測(cè)試

到此這篇關(guān)于java實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的示例代碼的文章就介紹到這了,更多相關(guān)java身份證號(hào)驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Myeclipse部署Tomcat_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹了Myeclipse部署Tomcat的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07
java webApp異步上傳圖片實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java webApp異步上傳圖片實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
java通過(guò)SSE實(shí)現(xiàn)消息推送
這篇文章主要為大家詳細(xì)介紹了java如何通過(guò)SSE實(shí)現(xiàn)消息推送功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
IntelliJ IDEA創(chuàng)建maven多模塊項(xiàng)目(圖文教程)
這篇文章主要介紹了IntelliJ IDEA創(chuàng)建maven多模塊項(xiàng)目(圖文教程),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
Mybatis實(shí)現(xiàn)動(dòng)態(tài)拼接SQL的實(shí)踐指南
這篇文章主要為大家詳細(xì)介紹了Mybatis實(shí)現(xiàn)動(dòng)態(tài)拼接SQL的相關(guān)知識(shí),文中的示例代碼講解詳細(xì), 感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-11-11

