/**
* 通过身份证号码获取出生日期、性别、年龄
*
* @param idcard
* @return 返回的出生日期格式:1990-01-01 性别格式:0-男, 1-女
*/
public static Map<String, String> getBirAgeSex(String idcard) {
String birthday = "";
String age = "";
String sexCode = "";
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = idcard.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 && idcard.length() == 15) {
birthday = "19" + idcard.substring(6, 8) + "-" + idcard.substring(8, 10) + "-"
+ idcard.substring(10, 12);
sexCode = Integer.parseInt(idcard.substring(idcard.length() - 3, idcard.length()))
% 2 == 0 ? "1" : "0";
age = (year - Integer.parseInt("19" + idcard.substring(6, 8))) + "";
} else if (flag && idcard.length() == 18) {
birthday = idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-"
+ idcard.substring(12, 14);
sexCode = Integer.parseInt(idcard.substring(idcard.length() - 4, idcard.length() - 1))
% 2 == 0 ? "1" : "0";
age = (year - Integer.parseInt(idcard.substring(6, 10))) + "";
}
Map<String, String> map = new HashMap<String, String>();
map.put("birthday", birthday);
map.put("age", age);
map.put("gender", sexCode);
return map;
}