JAVA校驗(yàn)身份證號(hào)碼

/**

*

*/

import java.text.SimpleDateFormat;

/**

* @author liu

* @version 2017年4月5日 下午4:11:07

* @purpose

*/

public class Snippet {

private static boolean checkDate(String year, String month, String day) {

SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");

try {

String s3 = year + month + day;

simpledateformat.setLenient(false);

simpledateformat.parse(s3);

} catch (java.text.ParseException parseexception) {

return false;

}

return true;

}

/**

* 校驗(yàn)身份證

*

* @param certiCode

*? ? ? ? ? ? 待校驗(yàn)身份證

* @return 0--校驗(yàn)成功; 1--位數(shù)不對(duì); 2--生日格式不對(duì) ; 3--校驗(yàn)位不對(duì) ; 4--其他異常;5--字符異常;

* @param certiCode

* @return

*/

public static int checkCertiCode(String certiCode) {

try {

if (certiCode == null || certiCode.length() != 15

&& certiCode.length() != 18)

return 1;

String s1;

String s2;

String s3;

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

if (!checkFigure(certiCode)) {

return 5;

}

s1 = "19" + certiCode.substring(6, 10);

s2 = certiCode.substring(8, 10);

s3 = certiCode.substring(10, 12);

if (!checkDate(s1, s2, s3))

return 2;

}

if (certiCode.length() == 18) {

if (!checkFigure(certiCode.substring(0, 17))) {

return 5;

}

s1 = certiCode.substring(6, 10);

s2 = certiCode.substring(10, 12);

s3 = certiCode.substring(12, 14);

if (!checkDate(s1, s2, s3))

return 2;

if (!checkIDParityBit(certiCode))

return 3;

}

} catch (Exception exception) {

return 4;

}

return 0;

}

/**

* 檢查字符串是否全為數(shù)字

*

* @param certiCode

* @return

*/

private static boolean checkFigure(String certiCode) {

try {

Long.parseLong(certiCode);

} catch (NumberFormatException e) {

return false;

}

return true;

}

private static boolean checkIDParityBit(String certiCode) {

boolean flag = false;

if (certiCode == null || "".equals(certiCode))

return false;

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

if (certiCode.length() == 18) {

int i = 0;

for (int k = 0; k < 18; k++) {

char c = certiCode.charAt(k);

int j;

if (c == 'X'||c == 'x')

j = 10;

else if (c <= '9' || c >= '0')

j = c - 48;

else

return flag;

i += j * ai[k];

}

if (i % 11 == 1)

flag = true;

}

return flag;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,905評(píng)論 0 33
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,650評(píng)論 18 399
  • 一、 1、請(qǐng)用Java寫一個(gè)冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨(dú)云閱讀 1,496評(píng)論 0 6
  • java筆記第一天 == 和 equals ==比較的比較的是兩個(gè)變量的值是否相等,對(duì)于引用型變量表示的是兩個(gè)變量...
    jmychou閱讀 1,645評(píng)論 0 3
  • 2016.12.17 近3個(gè)月的目標(biāo)是: 升級(jí)我的伴侶和增加財(cái)富。用種子創(chuàng)造更加美好的工作和生活。我懷著無(wú)比喜悅、...
    朵兒2016閱讀 229評(píng)論 0 0

友情鏈接更多精彩內(nèi)容