根据身份证号码计算年龄(可扩展为工具类) /*计算年龄的工具类*/ 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) {//用户身份证为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); } } }