程序说明
可以根据用户输入的身份证号码,查询到身份证号码中的一些信息(如:归属地、性别、出生日期、校验位 等信息)
方法一(自己实现 身份证信息提取类 - IdCardIdentify )
package IDCard
.Identify
;
import java
.util
.Calendar
;
import java
.util
.regex
.Pattern
;
public class IdCardIdentify {
public void getInfo(String id
){
String province
= "", birthday
= "", gender
= "", Check_Digit
= "";
String province_str
= id
.substring(0, 2);
String birthday_str
= id
.substring(6, 14);
String gender_str
= id
.substring(16, 17);
String Check_Digit_str
= id
.substring(17, 18);
for(int i
=0;i
<cityCode
.length
;i
++) {
if(province_str
.equals(cityCode
[i
])) {
province
= codeAndCity
[i
][1];
}
}
birthday
= birthday_str
.substring(0, 4)+"年"+birthday_str
.substring(4, 6)+"月"+birthday_str
.substring(6, 8)+"日";
gender
= (Integer
.parseInt(gender_str
)%2==0)?"女":"男";
Check_Digit
= Check_Digit_str
;
Calendar cale
= Calendar
.getInstance();
int year
= cale
.get(Calendar
.YEAR
);
int age
= year
- Integer
.parseInt(birthday_str
.substring(0, 4));
System
.out
.println("身份证号码为 "+id
+" 的信息是");
System
.out
.println("省份为: "+province
);
System
.out
.println("生日为: "+birthday
);
System
.out
.println("年龄为: "+age
);
System
.out
.println("性别为: "+gender
);
System
.out
.println("校验位为: "+Check_Digit
);
}
protected String codeAndCity
[][] = { { "11", "北京" }, { "12", "天津" },
{ "13", "河北" }, { "14", "山西" }, { "15", "内蒙古" }, { "21", "辽宁" },
{ "22", "吉林" }, { "23", "黑龙江" }, { "31", "上海" }, { "32", "江苏" },
{ "33", "浙江" }, { "34", "安徽" }, { "35", "福建" }, { "36", "江西" },
{ "37", "山东" }, { "41", "河南" }, { "42", "湖北" }, { "43", "湖南" },
{ "44", "广东" }, { "45", "广西" }, { "46", "海南" }, { "50", "重庆" },
{ "51", "四川" }, { "52", "贵州" }, { "53", "云南" }, { "54", "西藏" },
{ "61", "陕西" }, { "62", "甘肃" }, { "63", "青海" }, { "64", "宁夏" },
{ "65", "新疆" }, { "71", "台湾" }, { "81", "香港" }, { "82", "澳门" },
{ "91", "国外" } };
private String cityCode
[] = { "11", "12", "13", "14", "15", "21", "22",
"23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43",
"44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63",
"64", "65", "71", "81", "82", "91" };
private int power
[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
private String verifyCode
[] = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
public boolean isValidate18Idcard(String idcard
) {
String idcard17
= idcard
.substring(0, 17);
String idcard18Code
= idcard
.substring(17, 18);
char c
[] = null
;
String checkCode
= "";
if (idcard
.length() != 18) {
System
.out
.println("身份证位数不对,理论18位,实际"+idcard
.length()+" 位!!!");
return false;
} else {
if (!is18Idcard(idcard
)) {
System
.out
.println("身份证类型不符合!!!");
return false;
} else {
boolean flag
= false;
for(int i
=0;i
<cityCode
.length
;i
++) {
if(idcard
.substring(0, 2).equals(cityCode
[i
])) {
flag
= true;
break;
}
}
if(!flag
) {
System
.out
.println("输入的是无效省份!!!");
return false;
}else {
String date_str
= idcard
.substring(6, 14);
int year_temp
= Integer
.parseInt(date_str
.substring(0, 4));
int month_temp
= Integer
.parseInt(date_str
.substring(4, 6));
int day_temp
= Integer
.parseInt(date_str
.substring(6, 8));
if(!valiDate(year_temp
, month_temp
, day_temp
)) {
System
.out
.println("出生日期不合法!!!");
return false;
}else {
c
= idcard17
.toCharArray();
}
}
}
}
if (null
!= c
) {
int bit
[] = new int[idcard17
.length()];
bit
= converCharToInt(c
);
int sum17
= 0;
sum17
= getPowerSum(bit
);
checkCode
= getCheckCodeBySum(sum17
);
if (null
== checkCode
) {
return false;
}
if (!idcard18Code
.equalsIgnoreCase(checkCode
)) {
System
.out
.println("计算出来的校验位为 :"+ checkCode
+"; 实际的校验位为:"+idcard18Code
);
return false;
}
}
return true;
}
public String
getCheckCodeBySum(int sum17
) {
String checkCode
= null
;
switch (sum17
% 11) {
case 10:
checkCode
= "2";
break;
case 9:
checkCode
= "3";
break;
case 8:
checkCode
= "4";
break;
case 7:
checkCode
= "5";
break;
case 6:
checkCode
= "6";
break;
case 5:
checkCode
= "7";
break;
case 4:
checkCode
= "8";
break;
case 3:
checkCode
= "9";
break;
case 2:
checkCode
= "x";
break;
case 1:
checkCode
= "0";
break;
case 0:
checkCode
= "1";
break;
}
return checkCode
;
}
public int getPowerSum(int[] bit
) {
int sum
= 0;
if (power
.length
!= bit
.length
) {
return sum
;
}
for (int i
= 0; i
< bit
.length
; i
++) {
for (int j
= 0; j
< power
.length
; j
++) {
if (i
== j
) {
sum
= sum
+ bit
[i
] * power
[j
];
}
}
}
return sum
;
}
public int[] converCharToInt(char[] c
) throws NumberFormatException
{
int[] a
= new int[c
.length
];
int k
= 0;
for (char temp
: c
) {
a
[k
++] = Integer
.parseInt(String
.valueOf(temp
));
}
return a
;
}
public boolean is18Idcard(String idcard
) {
return Pattern
.matches("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$", idcard
);
}
public boolean isDigital(String str
) {
return str
== null
|| "".equals(str
) ? false : str
.matches("^[0-9]*$");
}
public boolean isIdValid(String id
) {
return id
== null
|| "".equals(id
) ? false : id
.matches("\\d{17}(?:\\d|x|X)$");
}
public boolean valiDate(int iYear
, int iMonth
, int iDate
) {
Calendar cal
= Calendar
.getInstance();
int year
= cal
.get(Calendar
.YEAR
);
int datePerMonth
;
int MIN
= 1850;
if (iYear
< MIN
|| iYear
>= year
) {
return false;
}
if (iMonth
< 1 || iMonth
> 12) {
return false;
}
switch (iMonth
) {
case 4:
case 6:
case 9:
case 11:
datePerMonth
= 30;
break;
case 2:
boolean dm
= ((iYear
% 4 == 0 && iYear
% 100 != 0) || (iYear
% 400 == 0)) && (iYear
> MIN
&& iYear
< year
);
datePerMonth
= dm
? 29 : 28;
break;
default:
datePerMonth
= 31;
}
return (iDate
>= 1) && (iDate
<= datePerMonth
);
}
}
测试类:
package IDCard
.Identify
;
public class MyTest {
public static void main(String
[] args
) {
IdCardIdentify idCardIdentify
= new IdCardIdentify();
String id
= "110101199003077299";
if(idCardIdentify
.isValidate18Idcard(id
)) {
System
.out
.println("有效的身份证号码");
idCardIdentify
.getInfo(id
);
}else {
System
.out
.println("无效的身份证号码");
}
}
}
运行结果:
方法二(调用第三方接口)
参考博客:身份证归属地查询免费api接口代码和我上述自己写的 身份证信息提取类 比较: 优点:该API可以更加精确的查询到身份证的归属地 缺点:需要联网话不多说,直接上代码(两个类)
IdCard 类 — 身份证实体类
package IDCard
.Identify2
;
public class IdCard {
private String idCard
;
private String born
;
private String sex
;
private String att
;
public String
getIdCard() {
return idCard
;
}
public void setIdCard(String idCard
) {
this.idCard
= idCard
;
}
public String
getBorn() {
return born
;
}
public void setBorn(String born
) {
this.born
= born
;
}
public String
getSex() {
return sex
;
}
public void setSex(String sex
) {
this.sex
= sex
;
}
public String
getAtt() {
return att
;
}
public void setAtt(String att
) {
this.att
= att
;
}
}
IdCardService类+ 测试函数
package IDCard
.Identify2
;
import java
.io
.UnsupportedEncodingException
;
import java
.net
.URL
;
import java
.net
.UnknownHostException
;
import javax
.xml
.parsers
.DocumentBuilder
;
import javax
.xml
.parsers
.DocumentBuilderFactory
;
import org
.w3c
.dom
.Document
;
import org
.w3c
.dom
.NodeList
;
import IDCard
.Identify2
.IdCard
;
public class IdCardService {
public static String
getIdCardDetail(String cardNo
) throws UnsupportedEncodingException
{
IdCard idcard
= getIdCardInfo(cardNo
);
StringBuffer news
= new StringBuffer();
if (idcard
!= null
) {
news
.append("所属地区:"+idcard
.getAtt()).append("\n");
news
.append("出生日期:"+idcard
.getBorn()).append("\n");
news
.append("性别:"+idcard
.getSex()).append("\n");
}
if(news
.length() == 0){
news
.append("身份证号码").append(cardNo
).append("不存在,请重新输入!");
}
return news
.toString();
}
public static IdCard
getIdCardInfo(String cardNo
){
URL url
= null
;
IdCard idCard
= new IdCard();
try{
DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
DocumentBuilder builder
= factory
.newDocumentBuilder();
url
= new URL("http://api.k780.com:88/?app=idcard.get&idcard="+cardNo
+"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml");
System
.out
.println(url
);
Document doc
= builder
.parse(url
.openStream());
NodeList node
= doc
.getElementsByTagName("result");
for(int i
=0;i
<node
.getLength();i
++){
String idcard
= "";
String born
= "";
String sex
= "";
String att
= "";
if(doc
.getElementsByTagName("idcard").item(i
).getFirstChild() != null
){
idcard
= doc
.getElementsByTagName("idcard").item(i
).getFirstChild().getNodeValue();
}
if(doc
.getElementsByTagName("born").item(i
).getFirstChild() != null
){
born
= doc
.getElementsByTagName("born").item(i
).getFirstChild().getNodeValue();
}
if(doc
.getElementsByTagName("sex").item(i
).getFirstChild() != null
){
sex
= doc
.getElementsByTagName("sex").item(i
).getFirstChild().getNodeValue();
}
if(doc
.getElementsByTagName("att").item(i
).getFirstChild() != null
){
att
= doc
.getElementsByTagName("att").item(i
).getFirstChild().getNodeValue();
}
idCard
.setIdCard(idcard
);
idCard
.setBorn(born
);
idCard
.setSex(sex
);
idCard
.setAtt(att
);
}
}catch(UnknownHostException uhe
) {
System
.out
.println("无法连接网络");
System
.out
.println(uhe
.getMessage());
uhe
.printStackTrace();
}catch(Exception e
){
e
.printStackTrace();
}
return idCard
;
}
public static void main(String
[] args
){
try {
System
.out
.print(getIdCardDetail("110101199003070054"));
} catch (UnsupportedEncodingException e
) {
e
.printStackTrace();
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
运行截图