JAVA:18位身份证号码验证工具类(识别性别和生日、计算年龄)
生活随笔
收集整理的這篇文章主要介紹了
JAVA:18位身份证号码验证工具类(识别性别和生日、计算年龄)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.ljl.util;import java.util.Calendar;
import java.util.stream.IntStream;/*** 身份證號碼驗證* 1、號碼的結構* 公民身份號碼是特征組合碼,由十七位數字本體碼和一位校驗碼組成。從左至右依次為:六位數字地址碼,* 八位數字出生日期碼,三位數字順序碼和一位數字校驗碼。* 2、地址碼(前六位數)* 表示編碼對象常住戶口所在縣(市、旗、區)的行政區劃代碼,按GB/T2260的規定執行。* 3、出生日期碼(第七位至十四位)* 表示編碼對象出生的年、月、日,按GB/T7408的規定執行,年、月、日代碼之間不用分隔符。* 4、順序碼(第十五位至十七位)* 表示在同一地址碼所標識的區域范圍內,對同年、同月、同日出生的人編定的順序號,* 順序碼的奇數分配給男性,偶數分配給女性。* 5、校驗碼(第十八位數)* (1)十七位數字本體碼加權求和公式 S = Sum(Ai Wi), i = 0, , 16 ,先對前17位數字的權求和 ;* Ai:表示第i位置上的身份證號碼數字值; Wi:表示第i位置上的加權因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2* (2)計算模 Y = mod(S, 11)* (3)通過模( 0 1 2 3 4 5 6 7 8 9 10)得到對應的校驗碼 Y:1 0 X 9 8 7 6 5 4 3 2*/
public class IdentityUtils {// 身份證校驗碼private static final int[] COEFFICIENT_ARRAY = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};// 身份證號的尾數規則private static final String[] IDENTITY_MANTISSA = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};private static final String IDENTITY_PATTERN = "^[0-9]{17}[0-9Xx]$";public static boolean isLegalPattern(String identity) {if (identity == null) {return false;}if (identity.length() != 18) {return false;}if (!identity.matches(IDENTITY_PATTERN)) {return false;}char[] chars = identity.toCharArray();long sum = IntStream.range(0, 17).map(index -> {char ch = chars[index];int digit = Character.digit(ch, 10);int coefficient = COEFFICIENT_ARRAY[index];return digit * coefficient;}).summaryStatistics().getSum();// 計算出的尾數索引int mantissaIndex = (int) (sum % 11);String mantissa = IDENTITY_MANTISSA[mantissaIndex];String lastChar = identity.substring(17);if (lastChar.equalsIgnoreCase(mantissa)) {return true;} else {return false;}}//計算年齡public static int getAgefromBirthTime(String birthTimeString){// 先截取到字符串中的年、月、日String strs[] = birthTimeString.trim().split("-");int selectYear = Integer.parseInt(strs[0]);int selectMonth = Integer.parseInt(strs[1]);int selectDay = Integer.parseInt(strs[2]);// 得到當前時間的年、月、日Calendar cal = Calendar.getInstance();int yearNow = cal.get(Calendar.YEAR);int monthNow = cal.get(Calendar.MONTH) + 1;int dayNow = cal.get(Calendar.DATE);// 用當前年月日減去生日年月日int yearMinus = yearNow - selectYear -1;int monthMinus = monthNow - selectMonth;int dayMinus = dayNow - selectDay;int age = yearMinus;if (yearMinus < 0) {// 選了未來的年份age = 0;} else if (yearMinus == 0) {// 同年的,要么為1,要么為0if (monthMinus < 0) {// 選了未來的月份age = 0;} else if (monthMinus == 0) {// 同月份的if (dayMinus < 0) {// 選了未來的日期age = 0;} else if (dayMinus >= 0) {age = 1;}} else if (monthMinus > 0) {age = 1;}} else if (yearMinus > 0) {if (monthMinus < 0) {// 當前月>生日月} else if (monthMinus == 0) {// 同月份的,再根據日期計算年齡if (dayMinus < 0) {} else if (dayMinus >= 0) {age = age + 1;}} else if (monthMinus > 0) {age = age + 1;}}return age;}
}
很好的校驗身份證工具類,忘記在哪個網站找的了,現保存一下以后可以再用到。
if (IdentityUtils.isLegalPattern(data.getArchivesIdcard())){//識別性別和生日并計算年齡String sex;if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判斷性別sex = "女";} else {sex = "男";}String birthday = "";birthday = data.getArchivesIdcard().substring(6,10)+"年"+data.getArchivesIdcard().substring(10,12)+"月"+data.getArchivesIdcard().substring(12,14)+"日";data.setArchivesBirthday(birthday);String birthDate = "";birthDate = data.getArchivesIdcard().substring(6,10)+"-"+data.getArchivesIdcard().substring(10,12)+"-"+data.getArchivesIdcard().substring(12,14);data.setArchivesAges(IdentityUtils.getAgefromBirthTime(birthDate)); }else{result.put("data", "");result.put("errcode", 1001);result.put("errmsg", "請檢查身份證是否正確");return result; }總結
以上是生活随笔為你收集整理的JAVA:18位身份证号码验证工具类(识别性别和生日、计算年龄)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用CSS中文字体转Unicode对照表
- 下一篇: 手机辐射对人体的危害