vue/java 感觉身份证号获取年龄

这段代码展示了如何在Vue和Java中通过身份证号码计算个人的年龄和性别。Vue部分使用JavaScript处理字符串来获取年龄,而Java部分则通过正则表达式验证身份证号码,并计算年龄和性别,最后将结果封装到Map中返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值