根据身份证号码计算年龄(可扩展为工具类)
public Integer CalcAgeByIdNumber(String idNumber){
int age;
String fyear,year,fyue,yue;
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
if(idNumber.length()==18) {
fyear = format.format(date).substring(0,4);
fyue = format.format(date).substring(5, 7);
year = idNumber.substring(6).substring(0, 4);
yue = idNumber.substring(10).substring(0, 2);
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(year);
}
}else {
fyear = format.format(date).substring(0,4);;
fyue = format.format(date).substring(5, 7);
year = "19" + idNumber.substring(6, 8);
yue = idNumber.substring(8, 10);
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(year);
}
}
}