- Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathIdCertificationUtil.java
More file actions
Latest commit
172 lines (157 loc) · 5.73 KB
/
Copy pathIdCertificationUtil.java
File metadata and controls
172 lines (157 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importlombok.extern.slf4j.Slf4j;
importorg.apache.commons.lang3.StringUtils;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
@Slf4j
publicclassIdCertificationUtil {
/** 性别表示的值 */
privatestaticfinalintMALE_SEX_INT = 1;
privatestaticfinalStringMALE_SEX_STRING = "男";
privatestaticfinalintFEMALE_SEX_INT = 2;
privatestaticfinalStringFEMALE_SEX_STRING = "女";
/** 第一代居民身份证长度 */
privatestaticfinalintFIRST_ID_CARD_LENGTH = 15;
/** 第一代居民身份证 年份值 */
privatestaticfinalStringFIRST_ID_CARD_YEAR = "19";
/** 第二代居民身份证长度 */
privatestaticfinalintSECOND_ID_CARD_LENGTH = 18;
/** 第二代居民身份证 校验码的模 */
privatestaticfinalintSECOND_ID_CARD_CHECK_MOD = 11;
/** 性别 map 中的 key 值 */
publicstaticfinalStringSEX_BY_INT_MAP_KEY = "sex_by_int";
publicstaticfinalStringSEX_BY_STRING_MAP_KEY = "sex_by_string";
privatestaticSimpleDateFormatformat = newSimpleDateFormat("yyyy-MM-dd");
/**
* @desc 通过身份证获取性别
* @auth llp
* @date 2022/7/15 16:10
* @param idCard 身份证
* @return java.util.Map<java.lang.String,java.lang.Object>
*/
publicstaticMap<String, Object> getSexFromIdCard(StringidCard){
Map<String, Object> sexMap = newHashMap<>();
// 默认值
intsexInt = 0;
StringsexStr = "未知";
// 15 位身份证
if (idCard.length() == FIRST_ID_CARD_LENGTH){
Stringsex = idCard.substring(14, 15);
// 偶数表示女性,奇数表示男性
if (Integer.parseInt(sex) % 2 == 0){
sexInt = FEMALE_SEX_INT;
sexStr = FEMALE_SEX_STRING;
}else {
sexInt = MALE_SEX_INT;
sexStr = MALE_SEX_STRING;
}
}
// 18 位身份证
if (idCard.length() == SECOND_ID_CARD_LENGTH){
Stringsex = idCard.substring(16, 17);
log.info("sex",sex);
// 偶数表示女性,奇数表示男性
if (Integer.parseInt(sex) % 2 == 0){
sexInt = FEMALE_SEX_INT;
sexStr = FEMALE_SEX_STRING;
}else {
sexInt = MALE_SEX_INT;
sexStr = MALE_SEX_STRING;
}
}
// 结果
sexMap.put(SEX_BY_INT_MAP_KEY, sexInt);
sexMap.put(SEX_BY_STRING_MAP_KEY, sexStr);
returnsexMap;
}
/**
* 根据身份证号获取年龄
* @param IDCard
* @return
*/
publicstaticIntegergetAge(StringIDCard){
Integerage = 0;
Datedate = newDate();
if (StringUtils.isNotBlank(IDCard)&& isValid(IDCard)){
//15位身份证号
if (IDCard.length() == FIRST_ID_CARD_LENGTH){
// 身份证上的年份(15位身份证为1980年前的)
Stringuyear = "19" + IDCard.substring(6, 8);
// 身份证上的月份
Stringuyue = IDCard.substring(8, 10);
// 当前年份
Stringfyear = format.format(date).substring(0, 4);
// 当前月份
Stringfyue = format.format(date).substring(5, 7);
if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
// 当前用户还没过生
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
}
//18位身份证号
}elseif(IDCard.length() == SECOND_ID_CARD_LENGTH){
// 身份证上的年份
Stringyear = IDCard.substring(6).substring(0, 4);
// 身份证上的月份
Stringyue = IDCard.substring(10).substring(0, 2);
// 当前年份
Stringfyear = format.format(date).substring(0, 4);
// 当前月份
Stringfyue = format.format(date).substring(5, 7);
// 当前月份大于用户出身的月份表示已过生日
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
// 当前用户还没过生日
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(year);
}
}
}
returnage;
}
/**
* 身份证验证
* @param id 号码内容
* @return 是否有效
*/
publicstaticbooleanisValid(Stringid){
BooleanvalidResult = true;
//校验长度只能为15或18
intlen = id.length();
if (len != FIRST_ID_CARD_LENGTH && len != SECOND_ID_CARD_LENGTH){
validResult = false;
}
//校验生日
if (!validDate(id)){
validResult = false;
}
returnvalidResult;
}
/**
* 校验生日
* @param id
* @return
*/
privatestaticbooleanvalidDate(Stringid)
{
try
{
Stringbirth = id.length() == 15 ? "19" + id.substring(6, 12) : id.substring(6, 14);
SimpleDateFormatsdf = newSimpleDateFormat("yyyyMMdd");
DatebirthDate = sdf.parse(birth);
if (!birth.equals(sdf.format(birthDate))){
returnfalse;
}
}
catch (ParseExceptione)
{
returnfalse;
}
returntrue;
}
}