//15位到18位的身份证号
public static boolean checkIdCard(String idCard) {
String regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
return Pattern.matches(regex, idCard);
}
/**
* 获取出生日期 yyyy年MM月dd日
*/
public static String getBirthday(String IDCard) {
String birthday = "";
String year = "";
String month = "";
String day = "";
//15位身份证号
if (checkIdCard(IDCard)) {
if (IDCard.length() == 15) {
// 身份证上的年份(15位身份证为1980年前的)
year = "19" + IDCard.substring(6, 8);
//身份证上的月份
month = IDCard.substring(8, 10);
//身份证上的日期
day = IDCard.substring(10, 12);
//18位身份证号
} else if (IDCard.length() == 18) {
// 身份证上的年份
year = IDCard.substring(6).substring(0, 4);
// 身份证上的月份
month = IDCard.substring(10).substring(0, 2);
//身份证上的日期
day = IDCard.substring(12).substring(0, 2);
}
birthday = year + "年" + month + "月" + day + "日";
return birthday;
} else {
return "";
}
}