1. 程式人生 > >Whale on the way

Whale on the way

package jsj.hhtc.ds.string;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.HashMap;

import java.util.Map;

/**

 * 身份證驗證的工具(支援5位或18位省份證)

 * 身份證號碼結構:

 * 17位數字和1位校驗碼:6位地址碼數字,8位生日數字,3位出生時間順序號,1位校驗碼。

 * 地址碼(前6位):表示物件常住戶口所在縣(市、鎮、區)的行政區劃程式碼,按GB/T2260的規定執行。

 *

出生日期碼,(第七位至十四位):表示編碼物件出生年、月、日,按GBGB/T7408的規定執行,年、月、日程式碼之間不用分隔符。

 * 順序碼(第十五位至十七位):表示在同一地址碼所標示的區域範圍內,對同年、同月、同日出生的人編訂的順序號,

 * 順序碼的奇數分配給男性,偶數分配給女性。

 * 校驗碼(第十八位數):

 * 十七位數字本體碼加權求和公式 s = sum(Ai*Wi), i = 0,,16,先對前17位數字的權求和;

 *  Ai:表示第i位置上的身份證號碼數字值.Wi:表示第i位置上的加權因.Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

 *

計算模 Y = mod(S, 11)

 * 通過模得到對應的校驗碼 Y: 0 1 2 3 4 5 6 7 8 9 10 校驗碼: 1 0 X 9 8 7 6 5 4 3 2

 */

publicclass IDCardUtil {

finalstatic Map<Integer, String> zoneNum = new HashMap<Integer, String>();

static {

zoneNum.put(11, "北京");

zoneNum.put(12, "天津");

zoneNum.put(13, "河北");

zoneNum

.put(14, "山西");

zoneNum.put(15, "內蒙古");

zoneNum.put(21, "遼寧");

zoneNum.put(22, "吉林");

zoneNum.put(23, "黑龍江");

zoneNum.put(31, "上海");

zoneNum.put(32, "江蘇");

zoneNum.put(33, "浙江");

zoneNum.put(34, "安徽");

zoneNum.put(35, "福建");

zoneNum.put(36, "江西");

zoneNum.put(37, "山東");

zoneNum.put(41, "河南");

zoneNum.put(42, "湖北");

zoneNum.put(43, "湖南");

zoneNum.put(44, "廣東");

zoneNum.put(45, "廣西");

zoneNum.put(46, "海南");

zoneNum.put(50, "重慶");

zoneNum.put(51, "四川");

zoneNum.put(52, "貴州");

zoneNum.put(53, "雲南");

zoneNum.put(54, "西藏");

zoneNum.put(61, "陝西");

zoneNum.put(62, "甘肅");

zoneNum.put(63, "青海");

zoneNum.put(64, "新疆");

zoneNum.put(71, "臺灣");

zoneNum.put(81, "香港");

zoneNum.put(82, "澳門");

zoneNum.put(91, "外國");

    }

finalstaticint[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

finalstaticint[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

/**

     * 身份證驗證

     *@param號碼內容

     *@return是否有效 null"" 都是false

     */

publicstaticboolean isIDCard(String certNo){

if(certNo == null || (certNo.length() != 15 && certNo.length() != 18))

returnfalse;

finalchar[] cs = certNo.toUpperCase().toCharArray();

//校驗位數

int power = 0;

for(int i=0; i<cs.length; i++){

if(i==cs.length-1 && cs[i] == 'X')

break;//最後一位可以Xx

if(cs[i]<'0' || cs[i]>'9')

returnfalse;

if(i < cs.length -1){

                power += (cs[i] - '0') * POWER_LIST[i];

            }

        }

//校驗區位碼

if(!zoneNum.containsKey(Integer.valueOf(certNo.substring(0,2)))){

returnfalse;

        }

//校驗年份

        String year = certNo.length() == 15 ? getIdcardCalendar() + certNo.substring(6,8) :certNo.substring(6, 10);

finalint iyear = Integer.parseInt(year);

if(iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR))

returnfalse;//1900年的PASS,超過今年的PASS

//校驗月份

        String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10,12);

finalint imonth = Integer.parseInt(month);

if(imonth <1 || imonth >12){

returnfalse;

        }

//校驗天數

        String day = certNo.length() ==15 ? certNo.substring(10, 12) : certNo.substring(12, 14);

finalint iday = Integer.parseInt(day);

if(iday < 1 || iday > 31)

returnfalse;      

//校驗"校驗碼"

if(certNo.length() == 15)

returntrue;

return cs[cs.length -1 ] == PARITYBIT[power % 11];

    }

privatestaticint getIdcardCalendar() {       

         GregorianCalendar curDay = new GregorianCalendar();

int curYear = curDay.get(Calendar.YEAR);

int year2bit = Integer.parseInt(String.valueOf(curYear).substring(2));         

return  year2bit;

    }    

}

3、測試類

package jsj.hhtc.ds.string.test;

import jsj.hhtc.ds.string.IDCardUtil;

publicclass Testidcard {

publicstaticvoid main(String[] args) { 

//http://sfz.ckd.cc/ 可以獲取身份證號

//String idcard="450981198802261753";

//String idcard="37083119910215723X";

//String idcard="332621680413871";

        String idcard="450981198802261754";

//String idcard="47083119910215723X";

        IDCardUtil idu = new IDCardUtil();

boolean mark = idu.isIDCard(idcard);

if(mark){

             System.out.println(idcard+"是合法身份證號");

if(idcard.length()==15){

                System.out.println("您的出生日期是:19"+idcard.substring(6,8));

             }

else

             {

                System.out.println("您的出生日期是:"+idcard.substring(6,10));

             }

if(idcard.charAt(idcard.length()-1)%2==1){

                System.out.println("您的性別是:男");

             }

else

             {

                System.out.println("您的性別是:女");

             }

         }

else

         {

             System.out.println(idcard+"是非合法身份證號");

         }

    }

}