怎样读取身份证信息(根据身份证获取信息的方法)
怎样读取身份证信息(根据身份证获取信息的方法)
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) {
}
}
获取籍贯,因为所有地区信息太多了,就不放在这里了,需要的可以评论,私发给你。
知是行之始,行是知之成。