当前位置:首页 > 开发 > 编程语言 > Java > 正文

JAVA版身份证获取性别、出生日期及年龄

发表于: 2015-02-08   作者:bijian1013   来源:转载   浏览:
摘要:         工作中需要根据身份证获取性别、出生日期及年龄,且要还要支持15位长度的身份证号码,网上搜索了一下,经过测试好像多少存在点问题,干脆自已写一个。 CertificateNo.java package com.bijian.study; import java.util.Calendar; import

        工作中需要根据身份证获取性别、出生日期及年龄,且要还要支持15位长度的身份证号码,网上搜索了一下,经过测试好像多少存在点问题,干脆自已写一个。

CertificateNo.java

package com.bijian.study;

import java.util.Calendar;
import java.util.regex.Pattern;

/**
 * 根据身份证获取性别、出生日期、年龄,支持15、18位身份证
 */
public class CertificateNo {

    public ResultDTO parseCertificateNo(String certificateNo) {
        
        ResultDTO resultDTO = new ResultDTO();
        String myRegExpIDCardNo = "^\\d{6}(((19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\\d{3}([0-9]|x|X))|(\\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\\d{3}))$";
        boolean valid=Pattern.matches(myRegExpIDCardNo,certificateNo)||(certificateNo.length() == 17 && Pattern.matches(myRegExpIDCardNo,certificateNo.substring(0,15)));
        if(!valid){
            resultDTO.setStatueMessage("证件号码不规范!");
            return resultDTO;
        }
        int idxSexStart = 16;
        int birthYearSpan = 4;
        //如果是15位的证件号码
        if(certificateNo.length() == 15) {
            idxSexStart = 14;
            birthYearSpan = 2;
        }
        
        //性别
        String idxSexStr = certificateNo.substring(idxSexStart, idxSexStart + 1);
        int idxSex = Integer.parseInt(idxSexStr) % 2;
        String sex = (idxSex == 1) ? "M" : "F";
        resultDTO.setSex(sex);
        
        //出生日期
        String year = (birthYearSpan == 2 ? "19" : "") + certificateNo.substring(6, 6 + birthYearSpan);
        String month = certificateNo.substring(6 + birthYearSpan, 6 + birthYearSpan + 2);
        String day = certificateNo.substring(8 + birthYearSpan, 8 + birthYearSpan + 2);
        String birthday = year + '-' + month + '-' + day;
        resultDTO.setBirthday(birthday);
        
        //年龄
        Calendar certificateCal = Calendar.getInstance();
        Calendar currentTimeCal = Calendar.getInstance();
        certificateCal.set(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day));
        int yearAge = (currentTimeCal.get(currentTimeCal.YEAR)) - (certificateCal.get(certificateCal.YEAR));
        certificateCal.set(currentTimeCal.get(Calendar.YEAR), Integer.parseInt(month)-1, Integer.parseInt(day));
        int monthFloor = (currentTimeCal.before(certificateCal) ? 1 : 0);
        resultDTO.setAge(yearAge - monthFloor);
        
        return resultDTO;
    }
}

ResultDTO.java

package com.bijian.study;

public class ResultDTO {
    
    private String statueMessage;
    private String sex;
    private String birthday;
    private int age;
    
    public String getStatueMessage() {
        return statueMessage;
    }
    public void setStatueMessage(String statueMessage) {
        this.statueMessage = statueMessage;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    public String toString() {
        String res = "";
        if(this.statueMessage != null) {
            res += this.statueMessage;
        }else {
            res += "sex:" + this.sex + ",birthday:" + this.birthday + ",age:" + this.age;
        }
        return res;
    }
}

CertificateNoTest.java

package com.bijian.test;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import com.bijian.study.CertificateNo;
import com.bijian.study.ResultDTO;

/**
 * 说明:
 * 1.身份证信息完全是测试时随便写的,如有雷同,纯属巧合
 * 2.写此测试案例的日期是2015-02-08,有测生日当天及前后的案例,后续再运行需更改
 */
public class CertificateNoTest {
    
    private CertificateNo certificateNo;
    private ResultDTO resultDTO;
    
    @Before
    public void setUp() throws Exception {
        certificateNo = new CertificateNo();
    }

    @Test
    public void test_abnormality_certificateNo_of_18_digit_430522199812623535() {
        
        resultDTO = certificateNo.parseCertificateNo("430522199812623535");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("证件号码不规范!", resultDTO.getStatueMessage());   
    }
    
    @Test
    public void test_abnormality_certificateNo_of_18_digit_430522198813013210() {
        
        resultDTO = certificateNo.parseCertificateNo("430522198813013210");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("证件号码不规范!", resultDTO.getStatueMessage());   
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_430522199812101515() {
        
        resultDTO = certificateNo.parseCertificateNo("430522199812101515");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1998-12-10", resultDTO.getBirthday());  
        Assert.assertEquals(16, resultDTO.getAge());  
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_430522199812101595() {
        
        resultDTO = certificateNo.parseCertificateNo("430522199812101595");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1998-12-10", resultDTO.getBirthday());  
        Assert.assertEquals(16, resultDTO.getAge());  
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_430522199812101585() {
        
        resultDTO = certificateNo.parseCertificateNo("430522199812101585");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("F", resultDTO.getSex());
        Assert.assertEquals("1998-12-10", resultDTO.getBirthday());  
        Assert.assertEquals(16, resultDTO.getAge());  
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_430522198810103212() {
        
        resultDTO = certificateNo.parseCertificateNo("430522198810103212");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1988-10-10", resultDTO.getBirthday());  
        Assert.assertEquals(26, resultDTO.getAge());  
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_before_birthday() {
        
        //测试的时间是2015-02-08,表明昨天刚过生日
        resultDTO = certificateNo.parseCertificateNo("430522198802073210");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1988-02-07", resultDTO.getBirthday());  
        Assert.assertEquals(27, resultDTO.getAge());  
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_birthday() {
        
        //测试的时间是2015-02-08,表明当天正好生日
        resultDTO = certificateNo.parseCertificateNo("430522198802083210");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1988-02-08", resultDTO.getBirthday());  
        Assert.assertEquals(27, resultDTO.getAge());  
    }
    
    @Test
    public void test_normality_certificateNo_of_18_digit_after_birthday() {
        
        //测试的时间是2015-02-08,表明第二天才过生日
        resultDTO = certificateNo.parseCertificateNo("430522198802093210");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1988-02-09", resultDTO.getBirthday());  
        Assert.assertEquals(26, resultDTO.getAge());  
    }
    
    @Test
    public void test_abnormality_certificateNo_of_15_digit_130503671401001() {
        
        resultDTO = certificateNo.parseCertificateNo("130503671401001");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("证件号码不规范!", resultDTO.getStatueMessage());   
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_430522760201356() {
        
        resultDTO = certificateNo.parseCertificateNo("430522760201356");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("F", resultDTO.getSex());
        Assert.assertEquals("1976-02-01", resultDTO.getBirthday());  
        Assert.assertEquals(39, resultDTO.getAge());    
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_130503670401001() {
        
        resultDTO = certificateNo.parseCertificateNo("130503670401001");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1967-04-01", resultDTO.getBirthday());  
        Assert.assertEquals(47, resultDTO.getAge());    
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_370802940221002() {
        
        resultDTO = certificateNo.parseCertificateNo("370802940221002");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("F", resultDTO.getSex());
        Assert.assertEquals("1994-02-21", resultDTO.getBirthday());  
        Assert.assertEquals(20, resultDTO.getAge());    
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_370802941031331() {
        
        resultDTO = certificateNo.parseCertificateNo("370802941031331");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1994-10-31", resultDTO.getBirthday());  
        Assert.assertEquals(20, resultDTO.getAge());    
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_before_birthday() {
        
        //测试的时间是2015-02-08,表明昨天刚过生日
        resultDTO = certificateNo.parseCertificateNo("370802940207331");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1994-02-07", resultDTO.getBirthday());  
        Assert.assertEquals(21, resultDTO.getAge());    
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_birthday() {
        
        //测试的时间是2015-02-08,表明当天正好生日
        resultDTO = certificateNo.parseCertificateNo("370802940208331");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1994-02-08", resultDTO.getBirthday());  
        Assert.assertEquals(21, resultDTO.getAge());    
    }
    
    @Test
    public void test_normality_certificateNo_of_15_digit_after_birthday() {
        
        //测试的时间是2015-02-08,表明第二天才过生日
        resultDTO = certificateNo.parseCertificateNo("370802940209331");
        Assert.assertNotNull(resultDTO);   
        Assert.assertEquals("M", resultDTO.getSex());
        Assert.assertEquals("1994-02-09", resultDTO.getBirthday());  
        Assert.assertEquals(20, resultDTO.getAge());    
    }
}

 

JAVA版身份证获取性别、出生日期及年龄

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
CertificateNoTest.html <!DOCTYPE HTML> <html> <head> <meta charset="utf-
CertificateNoTest.html <!DOCTYPE HTML> <html> <head> <meta charset="utf-
要进行设置的表格类似下图所示: 要得到的效果是,当输入完身份证号码的时候,性别和出生日期字段,
要进行设置的表格类似下图所示: 要得到的效果是,当输入完身份证号码的时候,性别和出生日期字段,
在做的一个系统中,客户要求通过填写身份证自动识别并填充籍贯、出生年月、性别。 身份证号码简介:
CNN应用之性别、年龄识别 原文地址:http://blog.csdn.net/hjimce/article/details/49255013 作者:
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生
  前几段微软推出的大数据人脸识别年龄应用how-old.net在微博火了一把,它可以通过照片快速获得照
使用说明: //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
年龄大了学Java是爱好还是转型? 一、前言 35岁,好像年龄也不小了,工作也有十年多了,一直搞编程
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号