求个检验身份证合法的JAVA算法 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这程序我能给你.public class IdCheck { public IdCheck() { } /** * 修补15位居民身份证号码为18位 * * @param personIDCode * @return */ public static String fixPersonIDCode(String personIDCode) { String retIDCode = ""; if (personIDCode == null || personIDCode.trim().length() != 15) { return personIDCode; } String id17 = personIDCode.substring(0, 6) + "19" + personIDCode.substring(6, 15); // 15为身份证补\'19\' char[] code = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // 11个 int[] factor = { 0, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; // 18个; int[] idcd = new int[18]; int i; int j; int sum; int remainder; for (i = 1; i < 18; i++) { j = 17 - i; idcd[i] = Integer.parseInt(id17.substring(j, j + 1)); } sum = 0; for (i = 1; i < 18; i++) { sum = sum + idcd[i] * factor[i]; } remainder = sum % 11; String lastCheckBit = String.valueOf(code[remainder]); return id17 + lastCheckBit; } /** * 判断是否是有效的18位居民身份证号码 * * @param identityId:18位居民身份证号码 * @return:true: 有效的18位居民身份证号码 */ public boolean isIdentityId(String identityId) { if (isEmpty(identityId)) return false; try { if (identityId.length() == 18) { String identityId15 = identityId.substring(0, 6) + identityId.substring(8, 17); // System.out.println("the identityId15 is : "+identityId15); if (fixPersonIDCode(identityId15).equalsIgnoreCase(identityId)) { return true; } else { return false; } } else if (identityId.length() == 15) { try { Long.parseLong(identityId); return true; } catch (Exception ex) { return false; } } else { return false; } } catch (Exception ex) { return false; } } /** * 判断是否为空串"" */ public static boolean isEmpty(String sValue) { if (sValue == null) return true; return sValue.trim().equals("") ? true : false; } /** public static void main(String[] args) { try{ //System.out.println(fixPersonIDCode("130202198209060645")); //身份证15位转18位 System.out.println(isIdentityId ("130202198209060645")); //否是为有效的15位身份证号 } catch (Exception e) { e.printStackTrace(); } } **/} 50419253 java高手群里一起讨论 socket问题 急!swing jtree节点的问题! 有人测过1.4和1.5的虚拟机的性能吗?居然差几十倍?怎么回事儿? 关于使用java命令设置系统属性的问题。 新年好,散分求答案。 各位仁兄有没有能讲清楚Collection、Collections、Map、List、也就是util包里面这些东西的资料?我看了好多书,花了好找时间反而是越看越 jar文件问题 请问谁有俄罗斯方块的源代码! autoexec.bat和classpath的疑惑 jdbc显示数据库中的中文怎么是乱码啊! 大家一起提高! 想和我一样的初学者一起分享知识 大家一起提高!
public class IdCheck { public IdCheck() {
} /**
* 修补15位居民身份证号码为18位
*
* @param personIDCode
* @return
*/
public static String fixPersonIDCode(String personIDCode) {
String retIDCode = "";
if (personIDCode == null || personIDCode.trim().length() != 15) {
return personIDCode;
}
String id17 = personIDCode.substring(0, 6) + "19"
+ personIDCode.substring(6, 15); // 15为身份证补\'19\' char[] code = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // 11个
int[] factor = { 0, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; // 18个;
int[] idcd = new int[18];
int i;
int j;
int sum;
int remainder; for (i = 1; i < 18; i++) {
j = 17 - i;
idcd[i] = Integer.parseInt(id17.substring(j, j + 1));
} sum = 0;
for (i = 1; i < 18; i++) {
sum = sum + idcd[i] * factor[i];
}
remainder = sum % 11;
String lastCheckBit = String.valueOf(code[remainder]);
return id17 + lastCheckBit;
} /**
* 判断是否是有效的18位居民身份证号码
*
* @param identityId:18位居民身份证号码
* @return:true: 有效的18位居民身份证号码
*/
public boolean isIdentityId(String identityId) {
if (isEmpty(identityId))
return false;
try {
if (identityId.length() == 18) {
String identityId15 = identityId.substring(0, 6)
+ identityId.substring(8, 17);
// System.out.println("the identityId15 is : "+identityId15);
if (fixPersonIDCode(identityId15).equalsIgnoreCase(identityId)) {
return true;
} else {
return false;
}
} else if (identityId.length() == 15) {
try {
Long.parseLong(identityId);
return true;
} catch (Exception ex) {
return false;
}
} else {
return false;
}
} catch (Exception ex) {
return false;
} } /**
* 判断是否为空串""
*/
public static boolean isEmpty(String sValue) {
if (sValue == null)
return true;
return sValue.trim().equals("") ? true : false;
}
/**
public static void main(String[] args) {
try{
//System.out.println(fixPersonIDCode("130202198209060645")); //身份证15位转18位
System.out.println(isIdentityId ("130202198209060645")); //否是为有效的15位身份证号
} catch (Exception e) {
e.printStackTrace();
}
}
**/}