From 9e4fb13fec8bf2732f4567e7e72eeb9fd94c840c Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期日, 29 九月 2024 16:31:22 +0800 Subject: [PATCH] Merge branch 'master-stander' into master-jiayan --- hx_common/src/main/java/com/hx/util/IDCardUtil.java | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 163 insertions(+), 0 deletions(-) diff --git a/hx_common/src/main/java/com/hx/util/IDCardUtil.java b/hx_common/src/main/java/com/hx/util/IDCardUtil.java new file mode 100644 index 0000000..8cddcd4 --- /dev/null +++ b/hx_common/src/main/java/com/hx/util/IDCardUtil.java @@ -0,0 +1,163 @@ +package com.hx.util; + +import org.apache.commons.lang.StringUtils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +/** + * 韬唤璇佸彿鐮侀�昏緫鏍¢獙-鐢ㄤ簬涓婃姤鍖荤枟鏁版嵁 + * 璇ユ柟娉曟棤娉曚繚璇佹牎楠岀殑韬唤璇佷竴瀹氭槸鐪熷疄瀛樺湪鐨� + * */ +public class IDCardUtil { + + public static Boolean check(String idCardNo){ + if (StringUtils.isEmpty(idCardNo)){ + return false; + } + //灏嗗叾杞垚澶у啓鏈夌殑韬唤璇佹渶鍚庝竴浣嶆槸瀛楁瘝 + String idCard=idCardNo.toUpperCase(); + //15浣嶈韩浠借瘉杞垚18浣� + if (idCardNo.length() == 15) { + if (!(idCardNo.matches("[0-9]{17}[0-9|x]|[0-9]{15}"))) { + return false; + } + //15浣嶈浆鎹负18浣� + String s2 = idCardNo.substring(0, 6); + String s3 = idCardNo.substring(6, 15); + String changed = s2.concat("19").concat(s3); + idCard = changed.toUpperCase(); + } + //鑾峰彇韬唤璇佹渶鍚庝竴浣嶈繘琛岄獙璇� + String lastStr = idCard.substring(idCard.length() - 1); + //鑾峰彇韬唤璇佸墠17浣� + String firstStr = idCard.substring(0,17); + //楠岃瘉韬唤璇佸墠17浣嶆槸鍚︿负鏁板瓧 + boolean isDigits = Pattern.matches("^\\d{17}", firstStr); + if(!isDigits){ + return false; + } + //鍏ㄥ眬鍙橀噺 + if(idCardMap.isEmpty()){ + for(int i=0;i<=10;i++){ + idCardMap.put(i,idCardCheck[i]); + } + } + //鏄惁鍚堢悊 + boolean check=false; + + char[] idCardCharNumber = idCard.toCharArray(); + Integer resultSum = 0; + for (int i = 0; i < idCardCharNumber.length - 1; i++) { + resultSum += Character.getNumericValue(idCardCharNumber[i]) * idCardWeight[i]; + } + //灏嗙浉鍔犵殑鍓�17浣嶆暟瀛椾緷娆′箻浠ュ搴旂殑鏉冮噸鍥犲瓙鐩稿姞锛岀浉鍔犵殑缁撴灉闄や互11锛屽緱鍒颁綑鏁� + Integer lastResult = resultSum % 11; + //鏍规嵁浣欐暟锛屽搴斾竴涓寚瀹氱殑鏍¢獙鐮併�傛渶缁堝緱鍒扮殑鏍¢獙鐮佸氨鏄韩浠借瘉鍙风爜鐨勬渶鍚庝竴浣嶆暟瀛椼�� + // 閫氳繃杩欎釜鏍¢獙鐮侊紝鍙互楠岃瘉鍓嶉潰17浣嶆暟瀛楁槸鍚︽纭紝浠庤�屾彁楂樿韩浠借瘉鍙风爜鐨勫噯纭�� + if (idCardMap.get(lastResult) != null) { + check = idCardMap.get(lastResult).equals(lastStr); + } + return check; + } + + + + /**韬唤璇佸墠17浣嶆暟瀛椾緷娆′箻浠ュ搴旂殑鏉冮噸鍥犲瓙*/ + public static final Integer[] idCardWeight= {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; + /**韬唤璇佹渶鍚庝竴浣嶅搴旂殑鏍¢獙鐮�*/ + public static final String[] idCardCheck= {"1","0","X","9","8","7","6","5","4","3","2"}; + /**鏄熷骇鏁扮粍*/ + public static final String[] CONSTELLATION_ARR = { "姘寸摱搴�", "鍙岄奔搴�", "鐧界緤搴�", "閲戠墰搴�", "鍙屽瓙搴�", "宸ㄨ煿搴�", "鐙瓙搴�", "澶勫コ搴�", "澶╃Г搴�", "澶╄潕搴�", "灏勬墜搴�", "榄旂警搴�" }; + /**鏄熷骇瀵瑰簲鐨勮竟缂樻棩鏈�*/ + public static final int[] CONSTELLATION_EDGE_DAY = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22}; + /**鐢熻倴*/ + public static final String[] ZODIAC_ARR = { "鐚�", "楦�", "鐙�", "鐚�", "榧�", "鐗�", "铏�", "鍏�", "榫�", "铔�", "椹�", "缇�" }; + /**缁勮鏍规嵁浣欐暟锛屽搴斾竴涓寚瀹氱殑鏍¢獙鐮�*/ + private static Map<Integer,String> idCardMap=new HashMap<>(); + + /**鏍规嵁鏃ユ湡鑾峰彇鏄熷骇*/ + private static String getConstellation(Date date) { + if (date == null) { + return ""; + } + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + int month = cal.get(Calendar.MONTH); + int day = cal.get(Calendar.DAY_OF_MONTH); + if (day < CONSTELLATION_EDGE_DAY[month]) { + month = month - 1; + } + if (month >= 0) { + return CONSTELLATION_ARR[month]; + } + // default to return 榄旂警 + return CONSTELLATION_ARR[11]; + } + + /**鏍规嵁韬唤璇佸彿鍒ゆ柇鐢ㄦ埛鏄熷骇*/ + private static String getConstellation(String cardNo) { + // 鑾峰彇鍑虹敓鏃ユ湡 + String birthday = cardNo.substring(6, 14); + Date birthdate = null; + try { + birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday); + return getConstellation(birthdate); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /**鏍规嵁鏃ユ湡鑾峰彇鐢熻倴*/ + private static String getZodica(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + return ZODIAC_ARR[cal.get(Calendar.YEAR) % 12]; + } + + /**鏍规嵁韬唤璇佸彿鍒ゆ柇鐢ㄦ埛鐢熻倴*/ + private static String getZodica(String cardNo) { + // 鑾峰彇鍑虹敓鏃ユ湡 + String birthday = cardNo.substring(6, 14); + Date birthdate = null; + try { + birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday); + return getZodica(birthdate); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /**15浣嶈浆18浣嶈韩浠借瘉鍙�*/ + private static String get18Ic(String identityCard) { + String retId = ""; + String id17 = ""; + int sum = 0; + int y = 0; + // 瀹氫箟鏁扮粍瀛樻斁鍔犳潈鍥犲瓙锛坵eight factor锛� + int[] wf = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + // 瀹氫箟鏁扮粍瀛樻斁鏍¢獙鐮侊紙check code锛� + String[] cc = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; + if (identityCard.length() != 15) { + return identityCard; + } + // 鍔犱笂涓や綅骞�19 + id17 = identityCard.substring(0, 6) + "19" + identityCard.substring(6); + // 鍗佷竷浣嶆暟瀛楁湰浣撶爜鍔犳潈姹傚拰 + for (int i = 0; i < 17; i++) { + sum = sum + Integer.valueOf(id17.substring(i, i + 1)) * wf[i]; + } + // 璁$畻妯� + y = sum % 11; + // 閫氳繃妯″緱鍒板搴旂殑鏍¢獙鐮� cc[y] + retId = id17 + cc[y]; + return retId; + } +} -- Gitblit v1.8.0