java 校驗15位或18位身份證號碼
阿新 • • 發佈:2019-02-12
package com.first.common.utility.idcard;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class IDCardUtil {
final static Map<Integer, String> zoneNum = new HashMap<Integer, String>();
static {
zoneNum.put(11, "北京");
zoneNum.put(12, "天津");
zoneNum.put(13, "河北");
zoneNum.put(14, "山西");
zoneNum.put(15, "內蒙古");
zoneNum.put(21, "遼寧");
zoneNum.put(22, "吉林");
zoneNum.put(23, "黑龍江");
zoneNum.put(31, "上海");
zoneNum.put(32, "江蘇");
zoneNum.put(33, "浙江");
zoneNum.put(34, "安徽");
zoneNum.put(35, "福建");
zoneNum.put(36, "江西");
zoneNum.put(37, "山東");
zoneNum.put(41, "河南");
zoneNum.put(42, "湖北");
zoneNum.put(43, "湖南");
zoneNum.put(44, "廣東");
zoneNum.put(45, "廣西");
zoneNum.put(46, "海南");
zoneNum.put(50, "重慶");
zoneNum.put(51, "四川");
zoneNum.put(52, "貴州");
zoneNum.put(53, "雲南");
zoneNum.put(54, "西藏");
zoneNum.put(61, "陝西");
zoneNum.put(62, "甘肅");
zoneNum.put(63, "青海");
zoneNum.put(64, "新疆");
zoneNum.put(71, "臺灣");
zoneNum.put(81, "香港");
zoneNum.put(82, "澳門");
zoneNum.put(91, "外國");
}
final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
final static int[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
5, 8, 4, 2};
/**
* 身份證驗證
*@param s 號碼內容
*@return 是否有效 null和"" 都是false
*/
public static boolean isIDCard(String certNo){
if(certNo == null || (certNo.length() != 15 && certNo.length() != 18))
return false;
final char[] cs = certNo.toUpperCase().toCharArray();
//校驗位數
int power = 0;
for(int i=0; i<cs.length; i++){
if(i==cs.length-1 && cs[i] == 'X')
break;//最後一位可以 是X或x
if(cs[i]<'0' || cs[i]>'9')
return false;
if(i < cs.length -1){
power += (cs[i] - '0') * POWER_LIST[i];
}
}
//校驗區位碼
if(!zoneNum.containsKey(Integer.valueOf(certNo.substring(0,2)))){
return false;
}
//校驗年份
String year = null;
year = certNo.length() == 15 ? getIdcardCalendar(certNo):certNo.substring(6, 10);
final int iyear = Integer.parseInt(year);
if(iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR))
return false;//1900年的PASS,超過今年的PASS
//校驗月份
String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10,12);
final int imonth = Integer.parseInt(month);
if(imonth <1 || imonth >12){
return false;
}
//校驗天數
String day = certNo.length() ==15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
final int iday = Integer.parseInt(day);
if(iday < 1 || iday > 31)
return false;
//校驗"校驗碼"
if(certNo.length() == 15)
return true;
return cs[cs.length -1 ] == PARITYBIT[power % 11];
}
private static String getIdcardCalendar(String certNo){
// 獲取出生年月日
String birthday = certNo.substring(6, 12);
SimpleDateFormat ft = new SimpleDateFormat("yyMMdd");
Date birthdate = null;
try {
birthdate = ft.parse(birthday);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
Calendar cday = Calendar.getInstance();
cday.setTime(birthdate);
String year = String.valueOf(cday.get(Calendar.YEAR));
return year;
}
public static void main(String[] args) {
boolean istrue= isIDCard("330622810725323");
System.out.println(istrue);
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class IDCardUtil {
final static Map<Integer, String> zoneNum = new HashMap<Integer, String>();
static {
zoneNum.put(11, "北京");
zoneNum.put(12, "天津");
zoneNum.put(13, "河北");
zoneNum.put(14, "山西");
zoneNum.put(15, "內蒙古");
zoneNum.put(21, "遼寧");
zoneNum.put(22, "吉林");
zoneNum.put(23, "黑龍江");
zoneNum.put(31, "上海");
zoneNum.put(32, "江蘇");
zoneNum.put(33, "浙江");
zoneNum.put(34, "安徽");
zoneNum.put(35, "福建");
zoneNum.put(36, "江西");
zoneNum.put(37, "山東");
zoneNum.put(41, "河南");
zoneNum.put(42, "湖北");
zoneNum.put(43, "湖南");
zoneNum.put(44, "廣東");
zoneNum.put(45, "廣西");
zoneNum.put(46, "海南");
zoneNum.put(50, "重慶");
zoneNum.put(51, "四川");
zoneNum.put(52, "貴州");
zoneNum.put(53, "雲南");
zoneNum.put(54, "西藏");
zoneNum.put(61, "陝西");
zoneNum.put(62, "甘肅");
zoneNum.put(63, "青海");
zoneNum.put(64, "新疆");
zoneNum.put(71, "臺灣");
zoneNum.put(81, "香港");
zoneNum.put(82, "澳門");
zoneNum.put(91, "外國");
}
final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
final static int[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
5, 8, 4, 2};
/**
* 身份證驗證
*@param s 號碼內容
*@return 是否有效 null和"" 都是false
*/
public static boolean isIDCard(String certNo){
if(certNo == null || (certNo.length() != 15 && certNo.length() != 18))
return false;
final char[] cs = certNo.toUpperCase().toCharArray();
//校驗位數
int power = 0;
for(int i=0; i<cs.length; i++){
if(i==cs.length-1 && cs[i] == 'X')
break;//最後一位可以 是X或x
if(cs[i]<'0' || cs[i]>'9')
return false;
if(i < cs.length -1){
power += (cs[i] - '0') * POWER_LIST[i];
}
}
//校驗區位碼
if(!zoneNum.containsKey(Integer.valueOf(certNo.substring(0,2)))){
return false;
}
//校驗年份
String year = null;
year = certNo.length() == 15 ? getIdcardCalendar(certNo):certNo.substring(6, 10);
final int iyear = Integer.parseInt(year);
if(iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR))
return false;//1900年的PASS,超過今年的PASS
//校驗月份
String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10,12);
final int imonth = Integer.parseInt(month);
if(imonth <1 || imonth >12){
return false;
}
//校驗天數
String day = certNo.length() ==15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
final int iday = Integer.parseInt(day);
if(iday < 1 || iday > 31)
return false;
//校驗"校驗碼"
if(certNo.length() == 15)
return true;
return cs[cs.length -1 ] == PARITYBIT[power % 11];
}
private static String getIdcardCalendar(String certNo){
// 獲取出生年月日
String birthday = certNo.substring(6, 12);
SimpleDateFormat ft = new SimpleDateFormat("yyMMdd");
Date birthdate = null;
try {
birthdate = ft.parse(birthday);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
Calendar cday = Calendar.getInstance();
cday.setTime(birthdate);
String year = String.valueOf(cday.get(Calendar.YEAR));
return year;
}
public static void main(String[] args) {
boolean istrue= isIDCard("330622810725323");
System.out.println(istrue);
}
}