电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Android身份證號有效性校驗(yàn)工具類案例

瀏覽:38日期:2022-09-22 15:48:56

不記得從哪找的了,修改了部分代碼,修復(fù)在Android平臺下使用時,時區(qū)時間格式異常的問題。

package cn.aikongmeng.demo.utils;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Random;/** * Created by Arjun on 2017/4/25. * 身份證有效性校驗(yàn) */public class IdentityUtils { // 位權(quán)值數(shù)組 private static byte[] Wi = new byte[17]; // 身份證前部分字符數(shù) private static final byte fPart = 6; // 身份證算法求模關(guān)鍵值 private static final byte fMod = 11; // 舊身份證長度 private static final byte oldIDLen = 15; // 新身份證長度 private static final byte newIDLen = 18; // 新身份證年份標(biāo)志 private static final String yearFlag = '19'; // 校驗(yàn)碼串 private static final String CheckCode = '10X98765432'; // 最小的行政區(qū)劃碼 private static final int minCode = 150000; // 最大的行政區(qū)劃碼 private static final int maxCode = 700000;// 舊身份證號碼// private String oldIDCard='';// 新身份證號碼// private String newIDCard='';// 地區(qū)及編碼 //private String Area[][2] = private static void setWiBuffer() { for (int i = 0; i < Wi.length; i++) { int k = (int) Math.pow(2, (Wi.length - i)); Wi[i] = (byte) (k % fMod); } } //獲取新身份證的最后一位:檢驗(yàn)位 private static String getCheckFlag(String idCard) { int sum = 0; //進(jìn)行加權(quán)求和 for (int i = 0; i < 17; i++) { sum += Integer.parseInt(idCard.substring(i, i + 1)) * Wi[i]; } //取模運(yùn)算,得到模值 byte iCode = (byte) (sum % fMod); return CheckCode.substring(iCode, iCode + 1); } //判斷串長度的合法性 private static boolean checkLength(final String idCard, boolean newIDFlag) { boolean right = (idCard.length() == oldIDLen) || (idCard.length() == newIDLen); newIDFlag = false; if (right) { newIDFlag = (idCard.length() == newIDLen); } return right; } //獲取時間串 private static String getIDDate(final String idCard, boolean newIDFlag) { String dateStr = ''; if (newIDFlag) dateStr = idCard.substring(fPart, fPart + 8); else dateStr = yearFlag + idCard.substring(fPart, fPart + 6); return dateStr; } //判斷時間合法性 private static boolean checkDate(final String dateSource) { String dateStr = dateSource.substring(0, 4) + '-' + dateSource.substring(4, 6) + '-' + dateSource.substring(6, 8); System.out.println(dateStr); SimpleDateFormat df = new SimpleDateFormat('yyyy-MM-dd'); try { Date date = df.parse(dateStr); return (date != null); } catch (java.text.ParseException e) { e.printStackTrace(); return false; } } //舊身份證轉(zhuǎn)換成新身份證號碼 public static String getNewIDCard(final String oldIDCard) { //初始化方法 IdentityUtils.setWiBuffer(); if (!checkIDCard(oldIDCard)) { return oldIDCard; } String newIDCard = oldIDCard.substring(0, fPart); newIDCard += yearFlag; newIDCard += oldIDCard.substring(fPart, oldIDCard.length()); String ch = getCheckFlag(newIDCard); newIDCard += ch; return newIDCard; } //新身份證轉(zhuǎn)換成舊身份證號碼 public static String getOldIDCard(final String newIDCard) { //初始化方法 IdentityUtils.setWiBuffer(); if (!checkIDCard(newIDCard)) { return newIDCard; } String oldIDCard = newIDCard.substring(0, fPart) +newIDCard.substring(fPart + yearFlag.length(), newIDCard.length() - 1); return oldIDCard; } //判斷身份證號碼的合法性 public static boolean checkIDCard(final String idCard) { //初始化方法 IdentityUtils.setWiBuffer(); int length = idCard.length(); boolean isNew; if (length == oldIDLen) isNew = false; else if (length == newIDLen) isNew = true; else return false; //String message = ''; if (!checkLength(idCard, isNew)) { //message = 'ID長度異常'; return false; } String idDate = getIDDate(idCard, isNew); if (!checkDate(idDate)) { //message = 'ID時間異常'; return false; } if (isNew) { String checkFlag = getCheckFlag(idCard); String theFlag = idCard.substring(idCard.length() - 1, idCard.length()); if (!checkFlag.equals(theFlag)) {//message = '新身份證校驗(yàn)位異常';return false; } } return true; } //獲取一個隨機(jī)的'偽'身份證號碼 public static String getRandomIDCard(final boolean idNewID) { //初始化方法 IdentityUtils.setWiBuffer(); Random ran = new Random(); String idCard = getAddressCode(ran) + getRandomDate(ran, idNewID) + getIDOrder(ran); if (idNewID) { String ch = getCheckFlag(idCard); idCard += ch; } return idCard; } //產(chǎn)生隨機(jī)的地區(qū)編碼 private static String getAddressCode(Random ran) { if (ran == null) { return ''; } else { int addrCode = minCode + ran.nextInt(maxCode - minCode); return Integer.toString(addrCode); } } //產(chǎn)生隨機(jī)的出生日期 private static String getRandomDate(Random ran, boolean idNewID) { // TODO Auto-generated method stub if (ran == null) { return ''; } int year = 0; if (idNewID) { year = 1900 + ran.nextInt(2017 - 1900); } else { year = 1 + ran.nextInt(99); } int month = 1 + ran.nextInt(12); int day = 0; if (month == 2) { day = 1 + ran.nextInt(28); } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { day = 1 + ran.nextInt(31); } else { day = 1 + ran.nextInt(30); } NumberFormat nf = NumberFormat.getIntegerInstance(); nf.setMaximumIntegerDigits(2); nf.setMinimumIntegerDigits(2); String dateStr = Integer.toString(year) + nf.format(month) + nf.format(day); return dateStr; } //產(chǎn)生隨機(jī)的序列號 private static String getIDOrder(Random ran) { // TODO Auto-generated method stub NumberFormat nf = NumberFormat.getIntegerInstance(); nf.setMaximumIntegerDigits(3); nf.setMinimumIntegerDigits(3); if (ran == null) { return ''; } else { int order = 1 + ran.nextInt(999); return nf.format(order); } } public IdentityUtils() { setWiBuffer(); } /** * @param args */ public static void main(String[] args) { boolean checkFlag = IdentityUtils.checkIDCard('512501197203035172'); System.out.println(checkFlag); }}

補(bǔ)充知識:Android 【身份證校驗(yàn)方法】已封裝 可以直接調(diào)用 可用

做項(xiàng)目的時候,有需要對用戶進(jìn)行身份證進(jìn)行校驗(yàn),苦于單獨(dú)寫一個比較麻煩,于是找度娘協(xié)助,試了幾個方法這個方法比較全面一些,比較實(shí)用。

記錄下這個,也感謝原作者,只是現(xiàn)在忘了在哪復(fù)制了

import android.text.TextUtils; /** * 身份證的工具類 */public class IdCardUtil { private String idCardNum = null; private static int IS_EMPTY = 1; private static int LEN_ERROR = 2; private static int CHAR_ERROR = 3; private static int DATE_ERROR = 4; private static int CHECK_BIT_ERROR = 5; private String[] errMsg = new String[]{'身份證完全正確!', '身份證為空!', '身份證長度不正確!', '身份證有非法字符!', '身份證中出生日期不合法!', '身份證校驗(yàn)位錯誤!'}; private int error = 0; /** * 構(gòu)造方法。 * * @param idCardNum */ public IdCardUtil(String idCardNum) { // super(); this.idCardNum = idCardNum.trim(); if (!TextUtils.isEmpty(this.idCardNum)) { this.idCardNum = this.idCardNum.replace('x', 'X'); } } public String getIdCardNum() { return idCardNum; } public void setIdCardNum(String idCardNum) { this.idCardNum = idCardNum; if (!TextUtils.isEmpty(this.idCardNum)) { this.idCardNum = this.idCardNum.replace('x', 'X'); } } /** * 得到身份證詳細(xì)錯誤信息。 * * @return 錯誤信息。 */ public String getErrMsg() { return this.errMsg[this.error]; } /** * 是否為空。 * * @return true: null false: not null; */ public boolean isEmpty() { if (this.idCardNum == null) return true; else return this.idCardNum.trim().length() > 0 ? false : true; } /** * 身份證長度。 * * @return */ public int getLength() { return this.isEmpty() ? 0 : this.idCardNum.length(); } /** * 身份證長度。 * * @return */ public int getLength(String str) { return this.isEmpty() ? 0 : str.length(); } /** * 是否是15位身份證。 * * @return true: 15位 false:其他。 */ public boolean is15() { return this.getLength() == 15; } /** * 是否是18位身份證。 * * @return true: 18位 false:其他。 */ public boolean is18() { return this.getLength() == 18; } /** * 得到身份證的省份代碼。 * * @return 省份代碼。 */ public String getProvince() { return this.isCorrect() == 0 ? this.idCardNum.substring(0, 2) : ''; } /** * 得到身份證的城市代碼。 * * @return 城市代碼。 */ public String getCity() { return this.isCorrect() == 0 ? this.idCardNum.substring(2, 4) : ''; } /** * 得到身份證的區(qū)縣代碼。 * * @return 區(qū)縣代碼。 */ public String getCountry() { return this.isCorrect() == 0 ? this.idCardNum.substring(4, 6) : ''; } /** * 得到身份證的出生年份。 * * @return 出生年份。 */ public String getYear() { if (this.isCorrect() != 0) return ''; if (this.getLength() == 15) { return '19' + this.idCardNum.substring(6, 8); } else { return this.idCardNum.substring(6, 10); } } /** * 得到身份證的出生月份。 * * @return 出生月份。 */ public String getMonth() { if (this.isCorrect() != 0) return ''; if (this.getLength() == 15) { return this.idCardNum.substring(8, 10); } else { return this.idCardNum.substring(10, 12); } } /** * 得到身份證的出生日子。 * * @return 出生日期。 */ public String getDay() { if (this.isCorrect() != 0) return ''; if (this.getLength() == 15) { return this.idCardNum.substring(10, 12); } else { return this.idCardNum.substring(12, 14); } } /** * 得到身份證的出生日期。 * * @return 出生日期。 */ public String getBirthday() { if (this.isCorrect() != 0) return ''; if (this.getLength() == 15) { return '19' + this.idCardNum.substring(6, 12); } else { return this.idCardNum.substring(6, 14); } } /** * 得到身份證的出生年月。 * * @return 出生年月。 */ public String getBirthMonth() { return getBirthday().substring(0, 6); } /** * 得到身份證的順序號。 * * @return 順序號。 */ public String getOrder() { if (this.isCorrect() != 0) return ''; if (this.getLength() == 15) { return this.idCardNum.substring(12, 15); } else { return this.idCardNum.substring(14, 17); } } /** * 得到性別。 * * @return 性別:1-男 2-女 */ public String getSex() { if (this.isCorrect() != 0) return ''; int p = Integer.parseInt(getOrder()); if (p % 2 == 1) { return '男'; } else { return '女'; } } /** * 得到性別值。 * * @return 性別:1-男 2-女 */ public String getSexValue() { if (this.isCorrect() != 0) return ''; int p = Integer.parseInt(getOrder()); if (p % 2 == 1) { return '1'; } else { return '2'; } } /** * 得到校驗(yàn)位。 * * @return 校驗(yàn)位。 */ public String getCheck() { if (!this.isLenCorrect()) return ''; String lastStr = this.idCardNum.substring(this.idCardNum.length() - 1); if ('x'.equals(lastStr)) { lastStr = 'X'; } return lastStr; } /** * 得到15位身份證。 * * @return 15位身份證。 */ public String to15() { if (this.isCorrect() != 0) return ''; if (this.is15()) return this.idCardNum; else return this.idCardNum.substring(0, 6) + this.idCardNum.substring(8, 17); } /** * 得到18位身份證。 * * @return 18位身份證。 */ public String to18() { if (this.isCorrect() != 0) return ''; if (this.is18()) return this.idCardNum; else return this.idCardNum.substring(0, 6) + '19' + this.idCardNum.substring(6) + this.getCheckBit(); } /** * 得到18位身份證。 * * @return 18位身份證。 */ public static String toNewIdCard(String tempStr) { if (tempStr.length() == 18) return tempStr.substring(0, 6) + tempStr.substring(8, 17); else return tempStr.substring(0, 6) + '19' + tempStr.substring(6) + getCheckBit(tempStr); } /** * 校驗(yàn)身份證是否正確 * * @return 0:正確 */ public int isCorrect() { if (this.isEmpty()) { this.error = IdCardUtil.IS_EMPTY; return this.error; } if (!this.isLenCorrect()) { this.error = IdCardUtil.LEN_ERROR; return this.error; } if (!this.isCharCorrect()) { this.error = IdCardUtil.CHAR_ERROR; return this.error; } if (!this.isDateCorrect()) { this.error = IdCardUtil.DATE_ERROR; return this.error; } if (this.is18()) { if (!this.getCheck().equals(this.getCheckBit())) {this.error = IdCardUtil.CHECK_BIT_ERROR;return this.error; } } return 0; } private boolean isLenCorrect() { return this.is15() || this.is18(); } /** * 判斷身份證中出生日期是否正確。 * * @return */ private boolean isDateCorrect() { /*非閏年天數(shù)*/ int[] monthDayN = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /*閏年天數(shù)*/ int[] monthDayL = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int month; if (this.is15()) { month = Integer.parseInt(this.idCardNum.substring(8, 10)); } else { month = Integer.parseInt(this.idCardNum.substring(10, 12)); } int day; if (this.is15()) { day = Integer.parseInt(this.idCardNum.substring(10, 12)); } else { day = Integer.parseInt(this.idCardNum.substring(12, 14)); } if (month > 12 || month <= 0) { return false; } if (this.isLeapyear()) { if (day > monthDayL[month - 1] || day <= 0)return false; } else { if (day > monthDayN[month - 1] || day <= 0)return false; } return true; } /** * 得到校驗(yàn)位。 * * @return */ private String getCheckBit() { if (!this.isLenCorrect()) return ''; String temp = null; if (this.is18()) temp = this.idCardNum; else temp = this.idCardNum.substring(0, 6) + '19' + this.idCardNum.substring(6); String checkTable[] = new String[]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}; int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}; int sum = 0; for (int i = 0; i < 17; i++) { String ch = temp.substring(i, i + 1); sum = sum + Integer.parseInt(ch) * wi[i]; } int y = sum % 11; return checkTable[y]; } /** * 得到校驗(yàn)位。 * * @return */ private static String getCheckBit(String str) { String temp = null; if (str.length() == 18) temp = str; else temp = str.substring(0, 6) + '19' + str.substring(6); String checkTable[] = new String[]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}; int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}; int sum = 0; for (int i = 0; i < 17; i++) { String ch = temp.substring(i, i + 1); sum = sum + Integer.parseInt(ch) * wi[i]; } int y = sum % 11; return checkTable[y]; } /** * 身份證號碼中是否存在非法字符。 * * @return true: 正確 false:存在非法字符。 */ private boolean isCharCorrect() { boolean iRet = true; if (this.isLenCorrect()) { byte[] temp = this.idCardNum.getBytes(); if (this.is15()) {for (int i = 0; i < temp.length; i++) { if (temp[i] < 48 || temp[i] > 57) { iRet = false; break; }} } if (this.is18()) {for (int i = 0; i < temp.length; i++) { if (temp[i] < 48 || temp[i] > 57) { if (i == 17 && temp[i] != 88) { iRet = false; break; } }} } } else { iRet = false; } return iRet; } /** * 判斷身份證的出生年份是否未閏年。 * * @return true :閏年 false 平年 */ private boolean isLeapyear() { String temp; if (this.is15()) { temp = '19' + this.idCardNum.substring(6, 8); } else { temp = this.idCardNum.substring(6, 10); } int year = Integer.parseInt(temp); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; else return false; }}

重要的是這個方法可以驗(yàn)證'X','x'。

以上這篇Android身份證號有效性校驗(yàn)工具類案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | 电动百叶窗,开窗器,电动遮阳百叶,电动开窗机生产厂家-徐州鑫友工控科技发展有限公司 | 压力变送器-上海武锐自动化设备有限公司 | uv固化机-丝印uv机-工业烤箱-五金蚀刻机-分拣输送机 - 保定市丰辉机械设备制造有限公司 | 导电银胶_LED封装导电银胶_半导体封装导电胶厂家-上海腾烁 | 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 精密交叉滚子轴承厂家,转盘轴承,YRT转台轴承-洛阳千协轴承 | C形臂_动态平板DR_动态平板胃肠机生产厂家制造商-普爱医疗 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 商秀—企业短视频代运营_抖音企业号托管 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | COD分析仪|氨氮分析仪|总磷分析仪|总氮分析仪-圣湖Greatlake | 书信之家_书信标准模板范文大全| 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 爱德华真空泵油/罗茨泵维修,爱发科-比其尔产品供应东莞/杭州/上海等全国各地 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 单电机制砂机,BHS制砂机,制沙机设备,制砂机价格-正升制砂机厂家 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 天然气分析仪-液化气二甲醚分析仪|传昊仪器 | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 蓝鹏测控平台 - 智慧车间系统 - 车间生产数据采集与分析系统 | 铣床|万能铣床|立式铣床|数控铣床|山东滕州万友机床有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 304不锈钢无缝管_不锈钢管厂家 - 隆达钢业集团有限公司 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 电动液压篮球架_圆管地埋式篮球架_移动平箱篮球架-强森体育 | 合肥展厅设计-安徽展台设计-合肥展览公司-安徽奥美展览工程有限公司 | 进口消泡剂-道康宁消泡剂-陶氏消泡剂-大洋消泡剂 | 深圳品牌设计公司-LOGO设计公司-VI设计公司-未壳创意 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 |