public static Map<String, String> getCardInfo(String cardNum)
throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar today = Calendar.getInstance();
Calendar birth = Calendar.getInstance();
String cardMonth = "",cardDay = "",sex = "";
int age = 0;
int length = cardNum.length();
if (length == 18) {
birth.setTime(format.parse(cardNum.substring(6, 14)));
age = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
cardMonth = birth.get(Calendar.MONTH) + 1 + "";
cardDay = birth.get(Calendar.DATE) + "";
birth.set(Calendar.YEAR, today.get(Calendar.YEAR));
if (birth.get(Calendar.DAY_OF_YEAR) <= today.get(Calendar.DAY_OF_YEAR)) {
age++;
}
if (Integer.parseInt(cardNum.substring(16,17)) % 2 == 0) {
sex = "2";
} else {
sex = "1";
}
} else if (length == 15) {
birth.setTime(format.parse("19" + cardNum.substring(6, 12)));
age = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
cardMonth = birth.get(Calendar.MONTH) + 1 + "";
cardDay = birth.get(Calendar.DATE) + "";
birth.set(Calendar.YEAR, today.get(Calendar.YEAR));
if (birth.get(Calendar.DAY_OF_YEAR) <= today.get(Calendar.DAY_OF_YEAR)) {
age++;
}
if (Integer.parseInt(cardNum.substring(14, 15)) % 2 == 0) {
sex = "2";
} else {
sex = "1";
}
}
Map<String, String> map = new HashMap<String, String>();
map.put("sex", sex);
map.put("age", age + "");
if (cardMonth.length() == 1) {
cardMonth = "0" + cardMonth;
}
map.put("birthday", cardMonth + "-" + cardDay);
return map;
}