Vue代码如下
getAge(){//根据身份证号获取年龄
var birthday = this.insuredIdNo.substring(6,10)+"-"+this.insuredIdNo.substring(10,12)+"-"+this.insuredIdNo.substring(12,14);
let birthdays = new Date(birthday.replace(/-/g, "/"));//被保险人生日
let d = new Date(this.T_EFFECTIVETIME.replace(/-/g, "/"));//保单起期
let age =
d.getFullYear() -
birthdays.getFullYear() -
(d.getMonth() < birthdays.getMonth() ||
(d.getMonth() == birthdays.getMonth() &&
d.getDate() < birthdays.getDate())? 1: 0);
console.log("年龄为"+age);
return age;
},
JAVA代码
/**
* 根据身份证获取年龄,性别,生日
* @param idNum
* @return <code>Map<String,String>{年龄, 性别(1:男,2:女), 生日};</code>
* @author
*/
public static Map<String, String> getAgeAndSexById(String idNum,String newDate) {
String age = "";
String sex = "";
String bir = "";
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getDefault());//获取系统当前时间
int currentYear = calendar.get(Calendar.YEAR);
if (idNum.matches("^\\d{15}$|^\\d{17}[\\dxX]$")) {
if (idNum.length() == 18) {
Pattern pattern = Pattern.compile("\\d{6}(\\d{4})\\d{6}(\\d{1})[\\dxX]{1}");
Matcher matcher = pattern.matcher(idNum);
if (matcher.matches()) {
// age = String.valueOf(currentYear - Integer.parseInt(matcher.group(1)));
sex = Integer.parseInt(matcher.group(2))%2==0?"2":"1";
bir = idNum.substring(6, 10)+"-"
+ idNum.substring(10, 12)+"-"
+ idNum.substring(12, 14);
age = getAgeByBirthDay(newDate, bir)+"";
}
} else if (idNum.length() == 15) {
Pattern p = Pattern.compile("\\d{6}(\\d{2})\\d{5}(\\d{1})\\d{1}");
Matcher m = p.matcher(idNum);
if (m.matches()) {
int year = Integer.parseInt(m.group(1));
year = 2000 + year;
if (year > 2020) {
year = year - 100;
}
age = String.valueOf(currentYear - year);
sex = Integer.parseInt(m.group(2))%2==0?"2":"1";
bir = "19" + idNum.substring(6, 8)+"-"
+ idNum.substring(8, 10) +"-"
+ idNum.substring(10, 12);
}
}
}
Map<String, String> reMap = new HashMap<String, String>();
reMap.put("age", age);
reMap.put("sex", sex);
reMap.put("bir", bir);
return reMap;
}
/**
* 判断证件号是否正确
* @param idNo
* @return
*/
public static boolean ifIdNo(String idNo){
String b18 = "^[1-9]\\d{5}(18|19|2([0-9]))\\d{2}(0[0-9]|10|11|12)([0-2][1-9]|30|31)\\d{3}[0-9Xx]$";
String b15 = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$";
if(StringUtil.match(b18, idNo) || StringUtil.match(b15, idNo)){
return true;
}else{
return false;
}
}
**
* 根据生日获取年龄
* @param birthDay 生日
* @return
*/
public static int getAgeByBirthDay(String newDate, String birthDay){
if (birthDay == null || birthDay.length()<4) {
return 0;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// //得到当前的年份
// String cYear = sdf.format(new Date()).substring(0,4);
// String cMouth = sdf.format(new Date()).substring(5,7);
// String cDay = sdf.format(new Date()).substring(8,10);
//得到生日年份
String cYear = newDate .substring(0,4);
String cMouth = newDate .substring(5,7);
String cDay = newDate .substring(8,10);
//得到生日年份
String birth_Year = birthDay .substring(0,4);
String birth_Mouth = birthDay .substring(5,7);
String birth_Day = birthDay .substring(8,10);
//年月日比较后得到年龄
int age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth))<0) {
age=age-1;
}else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth))==0) {
if ( (Integer.parseInt(cDay) - Integer.parseInt(birth_Day))<0) {
age=age-1;
}else {
age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
}
}else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth))>0) {
age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
}
return age;
}