Java: 社會安全號碼碼15位18位互轉的類

來源:互聯網
上載者:User

package cyachina.util;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2004-8-13
 * Time: 15:37:28
 * To change this template use File | Settings | File Templates.
 */
public class IDCardUtil {

    /**
     *
     * @param century  19xx 年用 19,20xx 年用 20
     * @param idCardNo15 待轉換的 15 位社會安全號碼碼
     * @return
     */
    public static String from15to18(int century, String idCardNo15) {

        String centuryStr = "" + century;
        if(century <0 || centuryStr.length() != 2)
            throw new IllegalArgumentException("世紀數無效!應該是兩位的正整數。");
        if(!(isIdCardNo(idCardNo15) && idCardNo15.length() == 15))
            throw new IllegalArgumentException("舊的社會安全號碼格式不正確!");

        int[] weight = new int[] {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};

        // 通過加入世紀碼, 變成 17 為的新號碼本體.
        String newNoBody = idCardNo15.substring(0, 6) + centuryStr + idCardNo15.substring(6);

        //下面算最後一位校正碼

        int checkSum = 0;
        for(int i=0; i< 17; i++) {
            int ai = Integer.parseInt("" + newNoBody.charAt(i)); // 位於 i 位置的數值
            checkSum = checkSum + ai * weight[i];
        }

        int checkNum = checkSum % 11;
        String checkChar = null;

        switch(checkNum) {
            case 0: checkChar = "1"; break;
            case 1: checkChar = "0"; break;
            case 2: checkChar = "X"; break;
            default: checkChar = "" + (12 - checkNum);
        }

        return newNoBody + checkChar;

    }

    public static String from18to15(String idCardNo18) {

        if(!(isIdCardNo(idCardNo18) && idCardNo18.length() == 18))
            throw new IllegalArgumentException("社會安全號碼參數格式不正確!");

        return idCardNo18.substring(0, 6) + idCardNo18.substring(8, 17);
    }

    /**
     * 判斷給定的字串是不是符合社會安全號碼的要求
     * @param str
     * @return
     */
    public static boolean isIdCardNo(String str) {

        if(str == null)
            return false;

        int len = str.length();
        if(len != 15 && len != 18)
            return false;

        for(int i=0; i<len; i++) {
            try {
                Integer.parseInt("" + str.charAt(i));
            }
            catch(NumberFormatException e) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {

        System.out.println(from15to18(19, "XXXXXXXXXXXXXXX"));
        System.out.println(from18to15("XXXXXXXXXXXXXXXXXX"));
    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.