package com.hzrc.acl.authentication.security.util;import java.util.Calendar;public class getInfoByIdentityCodeUtil { private static boolean getValidIdentityCard(String identityCard){ if(identityCard == null || identityCard.length() == 0) return false; if(identityCard.length() != 18) return false; char[] identities = identityCard.toCharArray(); int code = 0; int[] number = new int[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1}; char[] numberArray = new char[]{'1','0','X','9','8','7','6','5','4','3','2'}; for(int i =0 ; i < 17; i ) { if(identities[i] < '0' || identities[i] > '9') { return false; } code = (identities[i] -'0') * number[i]; } code = code % 11; if(numberArray[code] != identities[17]) { return false; } return true; } public static Integer getGender(String identityCode) { if(identityCode == null || identityCode.length() == 0) return 0; int gender = 0; if(identityCode.length() == 18){ //如果身份证号18位,取身份证号倒数第二位 char c = identityCode.charAt(identityCode.length() - 2); gender = Integer.parseInt(String.valueOf(c)); }else{ //如果身份证号15位,取身份证号最后一位 char c = identityCode.charAt(identityCode.length() - 1); gender = Integer.parseInt(String.valueOf(c)); } if(gender % 2 == 1) return 1; return 2; } public static String getBirthday(String identityCode) { String birthday = null; if(identityCode == null || identityCode.length() == 0) return birthday; if (identityCode.length() == 15) { birthday = "19" identityCode.substring(6, 8) "-" identityCode.substring(8, 10) "-" identityCode.substring(10, 12); } else if (identityCode.length() == 18) { birthday = identityCode.substring(6, 10) "-" identityCode.substring(10, 12) "-" identityCode.substring(12, 14); } return birthday; } public static Integer getAge(String identityCode) { int age = 0; if(identityCode == null || identityCode.length() == 0) return age; int year = Calendar.getInstance().get(Calendar.YEAR); if (identityCode.length() == 15) { age = (year - Integer.parseInt("19" identityCode.substring(6, 8))); } else if (identityCode.length() == 18) { age = (year - Integer.parseInt(identityCode.substring(6, 10))); } return age; } public static String getNaticePlace(String identityCode) { if(identityCode == null || identityCode.length() == 0) return null; int nativePlaceCode=Integer.parseInt(identityCode.substring(0, 6)); return NativePlace.getNativePlace(nativePlaceCode); } public static void main(String[] args) { }}
获取籍贯,因为所有地区信息太多了,就不放在这里了,需要的可以评论,私发给你。
知是行之始,行是知之成。