校验身份证合法性(18位与15位互转)
生活随笔
收集整理的這篇文章主要介紹了
校验身份证合法性(18位与15位互转)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
身份證15位與18位互轉(zhuǎn)
最近在做一項(xiàng)目,用戶可能用的是18位身份證號(hào)查詢信息,但是登記的時(shí)候用的是15位身份證號(hào)登記的 ,想了好久最終,想到在后臺(tái)獲取到身份證號(hào),通過工具類轉(zhuǎn)化,18位轉(zhuǎn)15位,這樣取到兩種身份證號(hào),傳到數(shù)據(jù)庫用 OR 條件拼接就好了
package com.until; /*** 15位及18位身份證互轉(zhuǎn)類* @author * @date 2020/07/20**/ public class IDCardUtil {private IDCardUtil(){//todo}/*** 15位身份證轉(zhuǎn)18位* @param idCard* @return*///15位轉(zhuǎn)18位的,需要一個(gè)15位的身份證號(hào)參數(shù)public static String getIdCardfrom15to18(String idCard15){StringBuffer idCard18 = new StringBuffer(idCard15);// 校驗(yàn)碼值char[] checkBit = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3','2' };int sum = 0;if(idCard15.length() != 15){throw new RuntimeException("the length of idcard is not 15");}idCard18.insert(6, "19");for(int i = 0; i < idCard18.length(); i++){char c = idCard18.charAt(i);int num = c - '0';//獲取當(dāng)前權(quán)重int weight = (int)(Math.pow(2, idCard18.length() - i)) % 11;sum += num * weight;}//sum求模int index = sum % 11;//append校驗(yàn)碼idCard18.append(checkBit[index]);return idCard18.toString() ;}//18位轉(zhuǎn)15位的,需要一個(gè)18位的身份證號(hào)參數(shù)public static String getIdCardfrom18to15(String idCard18){StringBuffer idCard15 = new StringBuffer(idCard18);if(idCard15.length() != 18){throw new RuntimeException("the length of idcard is not 18");}idCard15.delete(17, 18);idCard15.delete(6, 8);return idCard15.toString();} }總結(jié)
以上是生活随笔為你收集整理的校验身份证合法性(18位与15位互转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HBase数据模型和表设计
- 下一篇: 市场营销必须收藏使用的5个软件