public class IDCardUtil {
private static final Integer FIFTEEN_ID_CARD = 15;
private static final Integer EIGHTEEN_ID_CARD = 18;
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public static Integer getSex(String IDCard) {
Integer sex = null;
if (IDCard != null) {
if (IDCard.length() == FIFTEEN_ID_CARD) {
if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
sex = 1;
} else {
sex = 0;
}
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) {
sex = 1;
} else {
sex = 0;
}
}
}
return sex;
}
public static Integer getAge(String IDCard) {
Integer age = 0;
Date date = new Date();
if (IDCard != null) {
if (IDCard.length() == FIFTEEN_ID_CARD) {
String uyear = "19" + IDCard.substring(6, 8);
String uyue = IDCard.substring(8, 10);
String fyear = format.format(date).substring(0, 4);
String fyue = format.format(date).substring(5, 7);
if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
}
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
String year = IDCard.substring(6).substring(0, 4);
String yue = IDCard.substring(10).substring(0, 2);
String fyear = format.format(date).substring(0, 4);
String fyue = format.format(date).substring(5, 7);
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(year);
}
}
}
return age;
}
public static String getBirthday(String IDCard) {
String birthday = "";
String year = "";
String month = "";
String day = "";
if (IDCard != null) {
if (IDCard.length() == FIFTEEN_ID_CARD) {
year = "19" + IDCard.substring(6, 8);
month = IDCard.substring(8, 10);
day = IDCard.substring(10, 12);
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
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;
}
}