/**
* 身份证解析工具
* @author kuroismile
* @since 2022.02.17
*/
public class IdnoUtils {
/**
* 获取性别
* @author kuroismile
* @since 2022.02.17
* @param idno 身份证号码
* @return gender 性别:0-女,1-男,值为空表示未知
*/
public static String getGender(String idno) {
String genderFlag;
int length = idno.length();
if (length == 18) {
// 18位身份证号,倒数第二位奇数为男,偶数为女
genderFlag = idno.substring(16, 17);
} else if (length == 15) {
// 15位身份证号,最后一位奇数为男,偶数为女
genderFlag = idno.substring(14);
} else {
return null;
}
return Integer.parseInt(genderFlag) % 2 == 0 ? "0" : "1";
}
/**
* 获取出生日期
* @author kuroismile
* @since 2022.02.17
* @param idno 身份证号码
* @return birthday 出生日期
*/
public static String getBirthday(String idno) {
String birthday = null;
int length = idno.length();
if (length == 18) {
// 18位身份证号
birthday= idno.substring(6, 14);
} else if (length == 15) {
// 15位身份证号
birthday= "19" + idno.substring(6, 12);
}
return birthday;
}
}
身份证号码解析
于 2022-02-17 12:38:30 首次发布