(Java background) to obtain birthday, age, sex (support 18-bit, 15-digit ID card) According to the ID card number __ back end

Source: Internet
Author: User
Package com.zysoft.own_demo.utils;
Import Java.text.SimpleDateFormat;

Import Java.util.Date; /** * @author Huang Jipo * @date 2018/1/18/public class Idcardutil {/** * get birthday * @param cardcode * @r
        Eturn */public static string Getbirthdaybyidcard (String cardcode) {Integer len = cardcode.length ();
        String Bir;
        if (len==18) {bir = GetBirthdayByIdCard18 (Cardcode);
        }else{bir = getBirthdayByIdCard15 (Cardcode);
    return BIR; /** * Get Age * @param cardcode * @return/public static string Getagebyidcard (String CARDC
        Ode) {Integer len = cardcode.length ();
        String age;
        if (len==18) {age = GetAgeByIdCard18 (Cardcode);
        }else{age = GetAgeByIdCard15 (Cardcode);
    return to age; /** * Access to Gender * @param cardcode * @return/public static string Getsexbyidcard (String CARDCOde) {Integer len = cardcode.length ();
        String sex;
        if (len==18) {sex = GetSexByIdCard18 (Cardcode);
        }else{sex = getSexByIdCard15 (Cardcode);
    return sex; /** * 18 ID * Get birthday * @param cardcode * @return * * private static String Getbirthdayb
        YIdCard18 (String cardcode) {//id date string idyear = Cardcode.substring (6). substring (0, 4)/year String Idyue = cardcode.substring. substring (0, 2);//Get month String Idday = cardcode.substring (a). substring
        (0, 2);//Get day String bir = idyear+ "-" +idyue + "-" + idday;
    return BIR; /** * 18 ID Card * Access to Gender * @param cardcode * @return * * private static String GETSEXBYIDCA Rd18 (String cardcode) {string sex = "2";//Unknown if (Integer.parseint (cardcode.substring). substring (0, 1)
        % 2 = 0) {//judge sex sex = "0";//woman} else {    Sex = "1";
    man} return sex; /** * 18 ID Card * Acquisition age * @param cardcode * @return * * private static String GETAGEBYIDCA Rd18 (String cardcode) {//id date string idyear = Cardcode.substring (6). substring (0, 4);//Get year S Tring Idyue = cardcode.substring. substring (0, 2);//Get month String Idday = cardcode.substring (a) substring (0, 2)

        //Get day String Idyr = Idyue + Idday + "";  Current Month Day string year = new SimpleDateFormat ("Yyyy-mm-dd"). Format (new Date ()). Substring (0, 4);//Current year String Yue = new SimpleDateFormat ("Yyyy-mm-dd"). Format (new Date ()). SUBSTRING (5, 7);//month String day = new Simpledateform
        at ("Yyyy-mm-dd"). Format (new Date ()). SUBSTRING (8, 10);

        String yr = Yue + day + "";
        int age = 0; if (Integer.parseint (Idyr) <= integer.parseint (yr)) {//indicates that the birthday is over age = integer.parseint (year)-Integer.pars
        Eint (idyear) + 1; else {//Birthday is not over age = integer.parseint (year)-Integer.parseint (idyear);
    Return to Age + ""; /** * 15 ID * Get birthday * @param cardcode * @return * * private static String Getbirthdayb
        YIdCard15 (String cardcode) {//id date String idyear = "n" + cardcode.substring (6, 8);
        String Idyue = cardcode.substring (8, 10);
        String idday = cardcode.substring (10, 12);
        String bir = idyear+ "-" +idyue + "-" + idday;
    return BIR; /** * 15 ID Card * Access to Gender * @param cardcode * @return * * private static String GETSEXBYIDCA Rd15 (String cardcode) {string sex = "2";//Unknown if (Integer.parseint (cardcode.substring (14, 15))% 2 = = 0)
    {//judge sex sex = "0";///woman} else {sex = "1";//man} return sex; /** * 15 ID Card * Acquisition age * @param cardcode * @return * * private static String GETAGEBYIDCA Rd15 (String Cardcode) {//ID card on the month-day String idyear = "a" + cardcode.substring (6, 8);
        String Idyue = cardcode.substring (8, 10);
        String idday = cardcode.substring (10, 12);

        String Idyr = Idyue + Idday + "";  Current Month Day string year = new SimpleDateFormat ("Yyyy-mm-dd"). Format (new Date ()). Substring (0, 4);//Current year String Yue = new SimpleDateFormat ("Yyyy-mm-dd"). Format (new Date ()). SUBSTRING (5, 7);//month String day = new Simpledateform
        at ("Yyyy-mm-dd"). Format (new Date ()). SUBSTRING (8, 10);

        String yr = Yue + day + "";
        int age = 0; if (Integer.parseint (Idyr) <= integer.parseint (yr)) {//indicates that the birthday is over age = integer.parseint (year)-Integer.pars
        Eint (idyear) + 1;
        else {//birthday is not over age = integer.parseint (year)-Integer.parseint (idyear);
    Return to Age + "";
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.