根据Java的Pattern和Matcher类通过分组解析出身份证的年月日:
public class GetDateInIdCard {
public static void main(String[] args) {
String idCard = "000000199103230000";
String dateReg = "\\d{6}(\\d{8})\\d{3}\\w";
String detailReg = "(\\d{4})(\\d{2})(\\d{2})";
Pattern pattern = Pattern.compile(dateReg);
Matcher matcher = pattern.matcher(idCard);
if(matcher.find()){
String date = matcher.group(1);
pattern = Pattern.compile(detailReg);
matcher = pattern.matcher(date);
if(matcher.find()){
System.out.println("年:\t"+matcher.group(1));
System.out.println("月:\t"+matcher.group(2));
System.out.println("日:\t"+matcher.group(3));
}
}
}
}
打印结果如下:
年: 1991
月: 03
日: 23
参考文章:Java正则表达式的应用