package com.hzrc.acl.authentication.security.util;import java.util.Calendar;/**
* 根据身份证号获取相关信息
*/public class getInfoByIdentityCodeUtil { /**
* 校验身份证的正确性
* @param identityCard
* @return
*/
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;
} /**
* 根据身份证号获取性别
* @param identityCode
* @return 0位置 1男生 2女生
*/
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;
} /**
* 根据身份证号获取出生年月
* @param identityCode
* @return
*/
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;
} /**
* 根据身份证号获取年龄
* @param identityCode
* @return
*/
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;
} /**
* 根据身份证号获取籍贯
* @param identityCode
* @return
*/
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) {
}
} ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei%
微信搜索公众号【深圳家】或者直接搜索【i-shenzhen】,获取更多信息 |