- package net.java2000.tools;
- import java.util.*;
- /**
- * <p>Title: 身份证辅助模块</p>
- * <p>Description: 负责身份证号码的审核以及数据提取</p>
- * <p>Copyright: Copyright (c) 2005</p>
- * <p>Company: java2000.net</p>
- * @author 赵学庆
- * @version 1.0
- */
- public class Certificate {
- public static final int[] IW = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
- public static final char[] szVerCode = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
- /**
- * 校验身份证号码的合法性
- * @param str String
- * @return boolean
- */
- public static boolean valid(String str) {
- // 字符串不能是null
- if (str == null) {
- return false;
- }
- // 去掉前后空格等无用字符
- str = str.trim();
- // 身份证只允许15位或18位
- if (str.length() != 15 && str.length() != 18) {
- return false;
- }
- // 暂时不判断15位的情况
- if (str.length() == 15) {
- return true;
- } else {
- // 判断18位号码的校验位
- return str.charAt(17) == getVerifyCode(str);
- }
- }
- /**
- * 根据字符串的前17位计算校验位
- * @param str String 字符串,至少17位长度
- * @return char 校验位(第18位)
- */
- public static char getVerifyCode(String str) {
- if (str != null && str.length() >= 17) {
- int IS = 0;
- for (int i = 0; i < 17; i++) {
- IS += (str.charAt(i) - '0') * IW[i];
- }
- return szVerCode[IS % 11];
- } else {
- return '-';
- }
- }
- /**
- * 读取身份证号码的生日信息
- * @param str String 身份证号码
- * @return Date 返回日期,如果不合法则返回null
- */
- public static Date getBirthday(String str) {
- if (valid(str)) {
- String birthday = str.length()==15?("19"+str.substring(6,12)):(str.substring(6,14));
- return DateTools.parse(birthday,"yyyyMMdd");
- } else {
- return null;
- }
- }
- /**
- * 返回身份证号码的性别信息
- * @param str String 身份证号码
- * @return int 返回1=男性,0=女性;-1=不合法
- */
- public static int getGender(String str) {
- if(valid(str)){
- return (str.length()==15?str.charAt(14):str.charAt(16))%2==1?1:0;
- }else{
- return -1;
- }
- }
- }
身份证辅助模块,审核以及数据提取
最新推荐文章于 2024-08-23 15:54:50 发布