package com.wondersgroup.kszx.util; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class IdCardUtil { private static final String regEx_15="(\b[0-9]{6})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{1})"; private static final String regEx_18="(\b[0-9]{6})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{1})([0-9]{1}|x|X\b)"; /** * 获取正则表达试 * @param card * @return */ public static String getregEx(String card){ if(card.toString().length()==15){ return regEx_15; }else if(card.toString().length()==18){ return regEx_18; }else{ return ""; } } /** * 验证身份证 * @param card * @return */ public static boolean validateCard(String card){ try{ if(card==null)return false; if(card.toString().length()!=15 && card.toString().length()!=18)return false; Pattern p=Pattern.compile(getregEx(card)); Matcher m=p.matcher(card); if(m.find()){ return true; }else{ return false; } }catch(Exception ex){ ex.printStackTrace(); return false; } } /** * 通过身份证解析性别 * @param card * @return */ public static String parseCardSex(String card){ String sex=""; int s=10; try{ if(validateCard(card)){ Pattern pp=Pattern.compile(getregEx(card)); Matcher mm=pp.matcher(card); while(mm.find()){ if(mm.groupCount()==6){ s=Integer.parseInt(mm.group(6)); }else if(mm.groupCount()==8){ s=Integer.parseInt(mm.group(7)); }else{ } } } if(s<10 && s%2==0){ sex="0"; }else if(s<10 && s%2==1){ sex="1"; }else{ sex="2"; } }catch(Exception ex){ ex.printStackTrace(); } return sex; } /** * * @param cardObj * @return */ public static String paresCardSex(Object cardObj){ return paresCardSex(cardObj); } /** * 通过身份证获取出生年月 * @param cardObj * @return */ public static String getBrithDayByCard(Object cardObj){ String date=""; try{ String card =cardObj.toString(); if(validateCard(card)){ Pattern pp=Pattern.compile(getregEx(card)); Matcher mm=pp.matcher(card); while(mm.find()){ if(mm.groupCount()==6){ date="19"+mm.group(2)+"-"+mm.group(3)+"-"+mm.group(4); }else if(mm.groupCount()==8){ date=mm.group(2)+mm.group(3)+"-"+mm.group(4)+"-"+mm.group(5); }else{ } } } }catch(Exception ex){ ex.printStackTrace(); } return date; } public static String trans15to18(String oriNo) { // if(oriNo==null||(oriNo.length()!=15 && oriNo.length()!=18)){ // return oriNo; // } // if(oriNo.length()==18){//211028 19 820909123 4 // oriNo=StringUtil.subString(oriNo,1,6)+StringUtil.subString(oriNo,9,9); // } if(oriNo==null||oriNo.length()!=15){ return oriNo; } int[] ai = new int[17]; int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; char[] veriCode = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; String str1 = oriNo.substring(0, 6); String str2 = oriNo.substring(6); oriNo = str1 + "19" + str2; char[] aiCh = oriNo.toCharArray(); for (int i = 0; i < ai.length; i++) { ai[i] = Character.getNumericValue(aiCh[i]); } int s = 0; for (int j = 0; j < ai.length; j++) { s = s + ai[j] * wi[j]; } int y = s % 11; char lastCh=veriCode[y]; return oriNo+Character.toString(lastCh); } /** * 身份证18位转15位 * @param s18 * @return * @throws Exception */ public static String trans18to15(String s18) throws Exception { if (s18 == null) { throw new Exception("trans18To15(): Error input ID number: ID number is null!"); } if (s18.length() != 18) { throw new Exception("trans18To15(): Error input ID number(" + s18 + "): ID.length()!=18"); } else { char[] chA = s18.toCharArray(); // 数据合法性检验 for (int i = 0; i < 18; i++) { if (chA[i] < '0' || chA[i] > '9') { if (i != 17) { throw new Exception("trans18To15(): Error input ID number(" + s18 + "): is not number!"); } else if (chA[i] != 'x' && chA[i] != 'X') { throw new Exception("trans18To15(): Error input ID number(" + s18 + "): is not number!"); } } } return s18.substring(0, 6) + s18.substring(8, 17); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // List list=new ArrayList(); // list.add("310113198403099890"); // for(int i=0;i<list.size();i++){ // System.out.println(IdCardUtil.trans15to18(list.get(i).toString())); // } // System.out.println(IdCardUtil.getBrithDayByCard("31011419840406301X")); System.out.println(IdCardUtil.trans15to18("310110580813423")); } }