需要编号文件
编号文件部分内容如下
11:北京市
1101:市辖区
110101:东城区
110102:西城区
110105:朝阳区
110106:丰台区
110107:石景山区
110108:海淀区......
编号文件内容比较多 csdn点击 下载地址
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位数字依次乘以对应的权重因子
public static final String[] CONSTELLATION_ARR = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};//星座数组
public static final int[] CONSTELLATION_EDGE_DAY = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};//星座对应的边缘日期
public static final String[] ZODIAC_ARR = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};//生肖
public static Map<Integer, String> idCardMap = new HashMap<>();//组装根据余数,对应一个指定的校验码
private static Map<Integer, String> nativePlaceCode = new HashMap<>(4096, 1);//内存籍贯编号,记录身份证编号对应的地址
public static void main(String[] args) throws Exception {
String path = "C:\\Users\\Administrator\\Desktop\\code.txt";//编号文件
init(path);//初始化身份证校验参数
String idCard = "512926164805034455";//测试的身份证号码
checkIdCard(idCard);//校验身份证是否输入正常
//基本信息
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));
}
/**
* 初始化地区编码文件与校验码
*
* @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);//将文件内容加载到map内存中
final String[] idCardCheck = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};//身份证最后一位对应的校验码
for (int i = 0; i < 10; i++) {
idCardMap.put(i, idCardCheck[i]);//校验码记录
}
}
}
/**
* 验证身份证号码是否正常
*
* @param idCardNo
* @return
*/
public static void checkIdCard(String idCardNo) throws Exception {
String idCard = idCardNo.toUpperCase();//将其转成大写有的身份证最后一位是字母
if (idCardNo.length() == 15) {//15位身份证转成18位
if (!(idCardNo.matches("[0-9]{17}[0-9|x]|[0-9]{15}"))) throw new Exception("身份证号码输入错误,请输入正确格式的15位身份证号码");
String s2 = idCardNo.substring(0, 6);//15位转换为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("身份证号码输入错误,前17位必须是数字");//验证身份证前17位是否为数字
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位数字依次乘以对应的权重因子相加,相加的结果除以11,得到余数
//根据余数,对应一个指定的校验码。最终得到的校验码就是身份证号码的最后一位数字。通过这个校验码,可以验证前面17位数字是否正确,从而提高身份证号码的准确性
if (!(idCardMap.containsKey(lastResult)) || !(idCardMap.get(lastResult).equals(idCard.substring(idCard.length() - 1)))) throw new Exception("身份证号码校验异常,输入错误");
}
/**
* 根据日期获取当前年龄
*
* @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;
}
/**
* 根据身份证id获取当前年龄
*
* @return
*/
public static String getSex(String idCard) {
String sex = Integer.parseInt(idCard.substring(16, 17)) % 2 == 0 ? "女" : "男";
return sex;
}
/**
* 根据身份证号判断用户星座
*
* @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;
}
/**
* 根据身份证号判断用户生肖
*
* @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();
}
}
}
运行结果 ,身份证是随意编写的,可以用自己的身份证进行测试