import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
try {
Test2 test2 = new Test2();
test2.getCarInfo("身份证号");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据身份证号计算年龄,获取性别
* @param CardCode
* @return
* @throws Exception
*/
public static Map<String, Object> getCarInfo(String CardCode)
throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
// 得到年份
String year = CardCode.substring(6).substring(0, 4);
// 得到月份
String yue = CardCode.substring(10).substring(0, 2);
String sex;
// 判断性别
if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
Date date = new Date();
// 得到当前的系统时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 当前年份
String fyear = format.format(date).substring(0, 4);
String fyue = format.format(date).substring(5, 7);
// 月份
// String fday=format.format(date).substring(8,10);
int age = 0;
// 当前月份大于用户出身的月份表示已过生日
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(year);
}
map.put("sex", sex);
map.put("age", age);
return map;
}
}