package com.czp;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IdentityCardUtil {
/***
* 验证身份证是否合法
* @param ID
* @return
*/
public boolean isIdentiy(String ID){
Pattern p=Pattern.compile("\\d{17}[0-9X]$|\\d{14}[0-9X]&");
return p.matcher(ID).matches();
}
/***
* 提取年月日
* @param speID
* @return
*/
public String getBtith(String speID){
Pattern p=Pattern.compile("\\d{6}(\\d{8}).*");
String res=null;
Matcher m=p.matcher(speID);
if(m.find()){
res=m.group(1);
}
return res;
}
/***
* 分别提取出年、月、日
* @param speStr
* @return
*/
public String[] getBrithAndDay(String speStr){
String[] st=new String[3];
Pattern p=Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");
Matcher m=p.matcher(speStr);
if(m.find()){
st[0]=m.group(1);
st[1]=m.group(2);
st[2]=m.group(3);
}
return st;
}
public static void main(String[] args) {
String ID="11012419891212X";
IdentityCardUtil IDU=new IdentityCardUtil();
boolean isID=IDU.isIdentiy(ID);
String[] st=null;
if(isID){
st=IDU.getBrithAndDay(IDU.getBtith(ID));
}
System.out.println(Arrays.toString(st));
}
}