国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android身份證號有效性校驗工具類案例

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

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

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. * 身份證有效性校驗 */public class IdentityUtils { // 位權值數組 private static byte[] Wi = new byte[17]; // 身份證前部分字符數 private static final byte fPart = 6; // 身份證算法求模關鍵值 private static final byte fMod = 11; // 舊身份證長度 private static final byte oldIDLen = 15; // 新身份證長度 private static final byte newIDLen = 18; // 新身份證年份標志 private static final String yearFlag = '19'; // 校驗碼串 private static final String CheckCode = '10X98765432'; // 最小的行政區劃碼 private static final int minCode = 150000; // 最大的行政區劃碼 private static final int maxCode = 700000;// 舊身份證號碼// private String oldIDCard='';// 新身份證號碼// private String newIDCard='';// 地區及編碼 //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); } } //獲取新身份證的最后一位:檢驗位 private static String getCheckFlag(String idCard) { int sum = 0; //進行加權求和 for (int i = 0; i < 17; i++) { sum += Integer.parseInt(idCard.substring(i, i + 1)) * Wi[i]; } //取模運算,得到模值 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; } } //舊身份證轉換成新身份證號碼 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; } //新身份證轉換成舊身份證號碼 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 = '新身份證校驗位異常';return false; } } return true; } //獲取一個隨機的'偽'身份證號碼 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; } //產生隨機的地區編碼 private static String getAddressCode(Random ran) { if (ran == null) { return ''; } else { int addrCode = minCode + ran.nextInt(maxCode - minCode); return Integer.toString(addrCode); } } //產生隨機的出生日期 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; } //產生隨機的序列號 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); }}

補充知識:Android 【身份證校驗方法】已封裝 可以直接調用 可用

做項目的時候,有需要對用戶進行身份證進行校驗,苦于單獨寫一個比較麻煩,于是找度娘協助,試了幾個方法這個方法比較全面一些,比較實用。

記錄下這個,也感謝原作者,只是現在忘了在哪復制了

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[]{'身份證完全正確!', '身份證為空!', '身份證長度不正確!', '身份證有非法字符!', '身份證中出生日期不合法!', '身份證校驗位錯誤!'}; private int error = 0; /** * 構造方法。 * * @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'); } } /** * 得到身份證詳細錯誤信息。 * * @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) : ''; } /** * 得到身份證的區縣代碼。 * * @return 區縣代碼。 */ 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'; } } /** * 得到校驗位。 * * @return 校驗位。 */ 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); } /** * 校驗身份證是否正確 * * @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() { /*非閏年天數*/ int[] monthDayN = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /*閏年天數*/ 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; } /** * 得到校驗位。 * * @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]; } /** * 得到校驗位。 * * @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; }}

重要的是這個方法可以驗證'X','x'。

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

標簽: Android
相關文章:
主站蜘蛛池模板: 一级黄色欧美片 | 精品91自产拍在线 | 久久免费观看国产精品 | 国产午夜三区视频在线 | 欧美综合自拍亚洲综合百度 | 毛片大片免费看 | 久久亚洲成人 | 青青热在线精品视频免费 | 日韩美女一级视频 | 午夜性爽快免费视频播放 | 免费看91毛片 | 午夜毛片免费观看视频 | 国产成人丝袜视频在线视频 | 久久道| 97国产免费全部免费观看 | 中文字幕一区二区三区视频在线 | 男女午夜爱爱久久无遮挡 | 欧美黄色网络 | 特级毛片a级毛免费播放 | 国产在线欧美日韩精品一区二区 | 一级特黄aaa免费 | 成年人在线视频网站 | 日本一区视频在线观看 | 8050网午夜一级毛片免费不卡 | 中文乱码字幕午夜无线观看 | 99re久久资源最新地址 | 色日韩在线 | 日本人成在线视频免费播放 | 最近中文在线中文 | 成年人网站在线观看免费 | 亚洲国产三级 | 久久99欧美| 中文字幕国产专区 | 极品色在线精品视频 | 欧美黄色免费网站 | 一区二区三区在线免费观看视频 | 欧美一级日韩一级亚洲一级 | 欧美成人高清手机在线视频 | 91久久精品一区二区三区 | 三级视频网站在线观看 | 色综合九九 |