java身份證驗證工具類

來源:互聯網
上載者:User

標籤:java-身份證驗證

此身份證工具類有如下功能:
1、驗證社會安全號碼碼是否合法。(16位或18位)
2、通過社會安全號碼碼計算年齡 。
3、通過社會安全號碼碼計算出生日期
4、通過社會安全號碼碼計算性別

package com.ciming.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class IDCardUtil {    private final static int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,            5, 8, 4, 2, 1 };    private final static int[] vi = { 1, 0, ‘X‘, 9, 8, 7, 6, 5, 4, 3, 2 };    private static int[] ai = new int[18];    /**     * 驗證身份證     *      * @param idcard     * @return     */    public static boolean VerifyIDCard(String idcard) {        if (idcard.length() == 15) {            idcard = uptoeighteen(idcard);        }        if (idcard.length() != 18) {            return false;        }        String verify = idcard.substring(17, 18);        if (verify.equals(getVerify(idcard))) {            return true;        }        return false;    }    private static String getVerify(String eightcardid) {        int remaining = 0;        if (eightcardid.length() == 18) {            eightcardid = eightcardid.substring(0, 17);        }        if (eightcardid.length() == 17) {            int sum = 0;            for (int i = 0; i < 17; i++) {                String k = eightcardid.substring(i, i + 1);                ai[i] = Integer.parseInt(k);            }            for (int i = 0; i < 17; i++) {                sum = sum + wi[i] * ai[i];            }            remaining = sum % 11;        }        return remaining == 2 ? "X" : String.valueOf(vi[remaining]);    }    private static String uptoeighteen(String fifteencardid) {        String eightcardid = fifteencardid.substring(0, 6);        eightcardid = eightcardid + "19";        eightcardid = eightcardid + fifteencardid.substring(6, 15);        eightcardid = eightcardid + getVerify(eightcardid);        return eightcardid;    }    /**     * 擷取性別     *      * @param idcard     * @return     */    public static String getSex(String idcard) {        String inputStr = idcard.toString();        int sex;        if (inputStr.length() == 18) {            sex = inputStr.charAt(16);            if (sex % 2 == 0) {                return "女";            } else {                return "男";            }        } else {            sex = inputStr.charAt(14);            if (sex % 2 == 0) {                return "女";            } else {                return "男";            }        }    }    /**     * 獲得出生日期     *      * @param idcard     * @return     * @throws ParseException      */    public static Date getBirthDate(String idcard) throws ParseException {        String year;        String month;        String day;        if (idcard.length() == 18) { // 處理18位身份證            year = idcard.substring(6, 10);            month = idcard.substring(10, 12);            day = idcard.substring(12, 14);        } else { // 處理非18位身份證            year = idcard.substring(6, 8);            month = idcard.substring(8, 10);            day = idcard.substring(10, 12);            year = "19" + year;        }        return new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + month                + "-" + day);    }    /**     * 根據身份證計算周歲     * @throws ParseException      */    public static int getAge(String idcard) throws ParseException {        Calendar calBirth = Calendar.getInstance();        Calendar today = Calendar.getInstance();        Date birthDate = getBirthDate(idcard);        calBirth.setTime(birthDate);        return getYearDiff(today, calBirth);    }    public static int getYearDiff(Calendar cal, Calendar cal1) {        int m = (cal.get(cal.MONTH)) - (cal1.get(cal1.MONTH));        int y = (cal.get(cal.YEAR)) - (cal1.get(cal1.YEAR));        return (y * 12 + m) / 12;    }    public static void main(String[] args) {    }}

java身份證驗證工具類

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.