/**
* 通过身份证号码获取出生日期、性别、年龄
*
* @param certificateNo
* @return 返回的出生日期格式:1990-01-01 性别格式:F-女,M-男
*/
public static Map<String, String> getBirAgeSex(String certificateNo) {
String birthday = "";
String age = "";
Integer sex = 0;
//判断身份证号是否合法
Boolean judgeId = judgeId(certificateNo);
if (judgeId == false) {
return new HashMap<String, String>();
}
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = certificateNo.toCharArray();
boolean flag = true;
//判断字符是否为数字
if (number.length == 15) {
for (int x = 0; x < number.length; x++) {
if (!flag) {
return new HashMap<String, String>();
}
flag = Character.isDigit(number[x]);
}
} else if (number.length == 18) {
for (int x = 0; x < number.length - 1; x++) {
if (!flag) {
return new HashMap<String, String>();
}
flag = Character.isDigit(number[x]);
}
}
//分别获取年月日 性别 年龄
if (flag && certificateNo.length() == 15) {
birthday = "19" + certificateNo.substring(6, 8) + "-"
+ certificateNo.substring(8, 10) + "-"
+ certificateNo.substring(10, 12);
sex = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length())) % 2 == 0 ? 2 : 1;
age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
} else if (flag && certificateNo.length() == 18) {
birthday = certificateNo.substring(6, 10) + "-"
+ certificateNo.substring(10, 12) + "-"
+ certificateNo.substring(12, 14);
sex = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? 2 : 1;
age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
}
Map<String, String> map = new HashMap<String, String>();
map.put("birthday", birthday);
map.put("age", age);
map.put("sex", sex.toString());
return map;
}
/**
* 根据身份证号判断性别
*
* @param idNumber
* @return
*/
public static Integer judgeGender(String idNumber) throws IllegalArgumentException {
System.out.println(idNumber.length());
if (idNumber.length() != 18 && idNumber.length() != 15) {
throw new IllegalArgumentException("身份证号长度错误");
}
int gender = 0;
if (idNumber.length() == 18) {
//如果身份证号18位,取身份证号倒数第二位
char c = idNumber.charAt(idNumber.length() - 2);
gender = Integer.parseInt(String.valueOf(c));
} else {
//如果身份证号15位,取身份证号最后一位
char c = idNumber.charAt(idNumber.length() - 1);
gender = Integer.parseInt(String.valueOf(c));
}
System.out.println("gender = " + gender);
if (gender % 2 == 1) {
return 1;
} else {
return 2;
}
}
/**
* 根据身份证号计算年龄
*
* @param idCard
* @return
*/
public static Integer countAge(String idCard) {
//判断身份证号是否合法
Boolean judgeId = judgeId(idCard);
if (judgeId == false) {
return 0;
}
//截取身份证中出行人出生日期中的年、月、日
Integer personYear = Integer.parseInt(idCard.substring(6, 10));
Integer personMonth = Integer.parseInt(idCard.substring(10, 12));
Integer personDay = Integer.parseInt(idCard.substring(12, 14));
Calendar cal = Calendar.getInstance();
// 得到当前时间的年、月、日
Integer yearNow = cal.get(Calendar.YEAR);
Integer monthNow = cal.get(Calendar.MONTH) + 1;
Integer dayNow = cal.get(Calendar.DATE);
// 用当前年月日减去生日年月日
Integer yearMinus = yearNow - personYear;
Integer monthMinus = monthNow - personMonth;
Integer dayMinus = dayNow - personDay;
Integer age = yearMinus + 1; //先大致赋值
if (yearMinus == 0) { //出生年份为当前年份
age = 0;
} else { //出生年份大于当前年份
if (monthMinus < 0) {//出生月份小于当前月份时,还没满周岁
age = age - 1;
}
if (monthMinus == 0) {//当前月份为出生月份时,判断日期
if (dayMinus < 0) {//出生日期小于当前月份时,没满周岁
age = age - 1;
}
}
}
System.out.println(age);
return age;
}
// 判断身份证号是否合法
public static Boolean judgeId(String id) {
Boolean result = true;
// 长度不等于 18 位
if (id.length() != 18) {
return false;
}
// 系数算法
String tempId = getStr(id, 0, 16);
int[] coeff = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char[] end = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int sum = 0;
for (int i = 0; i < tempId.length(); i++) {
int bye = tempId.charAt(i) - '0';
sum += bye * coeff[i];
}
sum %= 11;
if (end[sum] != getStr(id, 17, 17).charAt(0)) {
result = false;
}
return result;
}
// 截取字符串的方法
public static String getStr(String str, int a, int b) {
b++;
return str.substring(a, b);
}