您的位置:

身份证号相关操作

一、根据身份证号提取年龄

根据身份证号,可以提取出生年月日,通过当前日期计算年龄。

提取出生年月日:

public static String getBirthByIDCard(String idCard) {
    if (idCard.length() == 18) {
        return idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
    } else if (idCard.length() == 15) {
        return "19" + idCard.substring(6, 8) + "-" + idCard.substring(8, 10) + "-" + idCard.substring(10, 12);
    } else {
        return "";
    }
}

计算年龄:

public static int getAgeByIDCard(String idCard) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar dob = Calendar.getInstance();
        dob.setTime(sdf.parse(getBirthByIDCard(idCard)));
        Calendar today = Calendar.getInstance();
        int year = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        int month = today.get(Calendar.MONTH) - dob.get(Calendar.MONTH);
        int day = today.get(Calendar.DAY_OF_MONTH) - dob.get(Calendar.DAY_OF_MONTH);
        if (month < 0 || (month == 0 && day < 0)) {
            year--;
        }
        return year;
    } catch (ParseException e) {
        e.printStackTrace();
        return 0;
    }
}

二、根据身份证号提取出生日期

根据身份证号可以提取出生日期,根据身份证号的长度不同,提取出生日期的方式也不同。

身份证号为15位:

public static String getBirthByIDCard(String idCard) {
    if (idCard.length() == 15) {
        return "19" + idCard.substring(6, 8) + "-" + idCard.substring(8, 10) + "-" + idCard.substring(10, 12);
    } else {
        return "";
    }
}

身份证号为18位:

public static String getBirthByIDCard(String idCard) {
    if (idCard.length() == 18) {
        return idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
    } else {
        return "";
    }
}

三、根据身份证号如何查身份证有效期

身份证的有效期是从领证之日起计算的,以10年为一个周期,每个周期内的1、6月份是身份证的有效期截止日期,例如领证日期为:2008年10月1日,那么该身份证的有效期截止日期为:2018年10月1日。

public static String getIDCardValidDate(String idCard) {
    if (idCard.length() == 18) {
        String year = idCard.substring(6, 10);
        String month = idCard.substring(10, 12);
        String day = idCard.substring(12, 14);
        int age = Integer.parseInt(getAgeByIDCard(idCard));
        int validYear = Integer.parseInt(year) + age + (age >= 18 && age % 10 < Integer.parseInt(year.substring(2)) ? 10 : 0);
        return validYear + "-" + month + "-" + day;
    } else {
        return "";
    }
}

四、根据身份证号码提取年龄

与“根据身份证号提取年龄”相同。

五、根据身份证号查询车票

根据身份证号查询车票通常用于火车站、长途汽车站等场所,通过身份证号查询该人是否已买了车票以及车票的信息。

public static String getTicketInfoByIDCard(String idCard) {
    // TODO
}

六、根据身份证号提取性别

根据身份证号可以提取性别,身份证号的倒数第2位为奇数代表男性,偶数代表女性。

public static String getGenderByIDCard(String idCard) {
    if (idCard.length() == 18) {
        int genderCode = Integer.parseInt(idCard.substring(16, 17));
        if (genderCode % 2 == 0) {
            return "女";
        } else {
            return "男";
        }
    } else {
        return "";
    }
}

七、根据身份证号码查询个人信息

根据身份证号可以查询个人信息,如:姓名、出生年月、性别、籍贯等。

public static String getPersonInfoByIDCard(String idCard) {
    // TODO
}

八、根据身份证号计算年龄的公式

年龄的计算可以用以下公式:

年龄 = 当前年份 - 出生年份 -1 (到生日那年) + 出生年份所在世纪年份的增量

出生年份所在世纪年份的增量是指,每过一个世纪,这个增量就要加1。

public static int calculateAge(String birthDateStr) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar dob = Calendar.getInstance();
        dob.setTime(sdf.parse(birthDateStr));
        Calendar today = Calendar.getInstance();
        int year = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
            year--;
        }
        int century = Integer.parseInt(birthDateStr.substring(0, 2));
        int increment = (century - 19) / 4;
        return year - increment;
    } catch (ParseException e) {
        e.printStackTrace();
        return 0;
    }
}

九、根据身份证查手机号

根据身份证查手机号通常用于实名认证,通过身份证号查询该人的手机号。

public static String getMobileByIDCard(String idCard) {
    // TODO
}

十、根据身份证号码提取出生年月

与“根据身份证号提取出生日期”相同。