日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

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

瀏覽:115日期: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
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲少妇在线| 国产精品日韩| 欧美日韩精品免费观看视频完整| 日韩高清二区| 91九色精品国产一区二区| 欧美精品不卡| 国产精品自拍区| 精品亚洲美女网站| 欧美激情aⅴ一区二区三区 | 久久国产三级精品| 国产三级精品三级在线观看国产| 亚洲高清成人| 秋霞影院一区二区三区| 午夜在线视频观看日韩17c| 精品国产麻豆| 欧美黄页在线免费观看| 欧美专区一区| 91精品国产自产精品男人的天堂 | 免费在线小视频| 欧美天堂视频| 亚洲国产综合在线看不卡| 日韩在线第七页| 在线天堂中文资源最新版| 精品视频国产| 亚洲精品第一| 婷婷成人av| 欧美在线日韩| 欧美日韩中出| 麻豆国产欧美一区二区三区| 久久影院资源站| 日韩一区二区中文| 红桃视频欧美| 欧美亚洲三级| 免费在线成人| 日韩在线欧美| 免费视频久久| 免费亚洲婷婷| 婷婷激情一区| 激情视频一区二区三区| 久久久精品午夜少妇| 在线日韩视频| 成人va天堂| 亚洲深深色噜噜狠狠爱网站| 国产精品原创| 精精国产xxxx视频在线播放| 欧美一级精品| 亚洲一级大片| 日韩激情一区| 亚州精品视频| 国产不卡人人| 亚洲影院天堂中文av色| 麻豆国产精品| 亚洲欧美日韩精品一区二区| 国产探花一区二区| 精品一区二区三区在线观看视频| 麻豆精品久久久| 91高清一区| 精品一区二区三区的国产在线观看| 99久久久国产精品美女| 国产日韩免费| 国产精品3区| 蜜臀av性久久久久蜜臀aⅴ流畅| 日韩三级视频| 久久国产直播| 精品国产亚洲一区二区三区在线 | 亚洲香蕉久久| av高清一区| 国产伦精品一区二区三区在线播放| 91超碰国产精品| 国产成人精品三级高清久久91| 一区二区高清| 久久精品国产68国产精品亚洲| 久久av免费看| 日韩精品五月天| 男女男精品网站| 国产高清一区| 欧美粗暴jizz性欧美20| 国产精品久久久久久妇女 | 首页国产欧美久久| 国产在线不卡| 亚洲网站视频| 午夜日韩av| 欧美日韩三区| 欧美日韩精品一区二区三区视频| 久久成人高清| re久久精品视频| 国产精品亚洲欧美日韩一区在线| 国产精品久久久久蜜臀| 国产婷婷精品| 麻豆国产精品| 亚洲一区日本| 国产探花在线精品一区二区| 日韩在线综合| 国产亚洲人成a在线v网站| 日韩欧美一区二区三区在线视频| 亚洲一区二区日韩| 中文字幕成在线观看| 国产亚洲高清视频| 日本一区二区高清不卡| 在线精品福利| 99久久激情| 国产成人在线中文字幕| 日韩激情一区二区| 亚洲激精日韩激精欧美精品| 麻豆极品一区二区三区| 一区二区亚洲视频| 亚洲最新无码中文字幕久久| 日韩精品一级| 日韩精品一二区| 欧美va亚洲va日韩∨a综合色| 国产精品久久久久77777丨| 中文国产一区| 婷婷久久一区| 播放一区二区| 久久九九精品| 97人人精品| 国产suv精品一区二区四区视频 | 狠狠躁少妇一区二区三区| 青青国产精品| 99在线观看免费视频精品观看| 成人片免费看| 亚洲美女久久精品| 国产成人精品三级高清久久91| 欧美亚洲一区二区三区| 日日夜夜免费精品| 日韩精品电影一区亚洲| 在线观看视频免费一区二区三区| 亚洲福利国产| 欧美色图一区| 国精品一区二区三区| 亚洲福利精品| 香蕉成人久久| 另类av一区二区| 视频一区在线播放| 91久久中文| 久久午夜影视| 亚洲人成亚洲精品| 日韩精品视频在线看| 日韩成人午夜精品| 国产精品调教| 神马午夜在线视频| 99久久九九| 日韩在线一区二区| 日韩精品一区二区三区免费视频 | 亚洲女同av| 一区久久精品| 国产亚洲激情| 日韩有吗在线观看| 国产精品久久久久久久免费软件| 精品久久97| 亚洲深夜影院| 亚洲aa在线| 精品美女视频| 日韩午夜电影| 国产精品久久久久久久久久妞妞| 欧美freesex黑人又粗又大| 亚洲欧美日韩一区在线观看| 久久国际精品| 亚洲国产一区二区三区在线播放 | 六月婷婷一区| 手机精品视频在线观看| 国产欧美在线观看免费| 亚洲国产专区校园欧美| 国产伦理久久久久久妇女| 欧美一区三区| 国产亚洲精aa在线看| 一区二区亚洲精品| 久久97视频| 亚洲久久视频| 婷婷综合亚洲| 日韩精品不卡一区二区| 日本不卡在线视频| 国产精品日韩久久久| 日韩大片在线播放| 国产精品九九| 亚洲精品影院在线观看| 伊人久久大香线蕉av不卡| 国产精品美女午夜爽爽| 丝袜诱惑制服诱惑色一区在线观看| 欧美激情亚洲| 国产精品成人自拍| 日韩国产在线一| 老鸭窝毛片一区二区三区| 国产91一区| 99久久亚洲精品| 日韩精品一区二区三区免费观影| 精品91福利视频| 麻豆国产欧美日韩综合精品二区| 91国内精品| 欧美天堂一区| 日韩av中文字幕一区| 天堂成人国产精品一区| 亚洲一级在线| 伊人久久成人| 亚洲激情国产| 久久亚洲精品伦理| 麻豆亚洲精品| 一区二区三区国产盗摄| 亚洲小说春色综合另类电影| 鲁大师成人一区二区三区|