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
173
package com.yiboshi.science.utils;
import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Description:身份证号的util
* @Author: zdj
* @Date: Created in 2021-03-18
*/
public class IDCardUtil{
/**
* 15位身份证号
*/
private static final Integer FIFTEEN_ID_CARD=15;
/**
* 18位身份证号
*/
private static final Integer EIGHTEEN_ID_CARD=18;
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
/**
* 根据身份证号获取性别
* @param IDCard
* @return
*/
public static String getSex(String IDCard){
String sex ="";
if (StringUtils.isNotBlank(IDCard)){
//15位身份证号
if (IDCard.length() == FIFTEEN_ID_CARD){
if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
//18位身份证号
}else if(IDCard.length() == EIGHTEEN_ID_CARD){
// 判断性别
if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
}
}
return sex;
}
/**
* 根据身份证号获取年龄
* @param IDCard
* @return
*/
public static Integer getAge(String IDCard){
Integer age = 0;
Date date = new Date();
if (StringUtils.isNotBlank(IDCard)&& isValid(IDCard)){
//15位身份证号
if (IDCard.length() == FIFTEEN_ID_CARD){
// 身份证上的年份(15位身份证为1980年前的)
String uyear = "19" + IDCard.substring(6, 8);
// 身份证上的月份
String uyue = IDCard.substring(8, 10);
// 当前年份
String fyear = format.format(date).substring(0, 4);
// 当前月份
String fyue = 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位身份证号
}else if(IDCard.length() == EIGHTEEN_ID_CARD){
// 身份证上的年份
String year = IDCard.substring(6).substring(0, 4);
// 身份证上的月份
String yue = IDCard.substring(10).substring(0, 2);
// 当前年份
String fyear = format.format(date).substring(0, 4);
// 当前月份
String fyue = 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);
}
}
}
return age;
}
/**
* 获取出生日期 yyyy年MM月dd日
* @param IDCard
* @return
*/
public static String getBirthday(String IDCard){
String birthday="";
String year="";
String month="";
String day="";
if (StringUtils.isNotBlank(IDCard)){
//15位身份证号
if (IDCard.length() == FIFTEEN_ID_CARD){
// 身份证上的年份(15位身份证为1980年前的)
year = "19" + IDCard.substring(6, 8);
//身份证上的月份
month = IDCard.substring(8, 10);
//身份证上的日期
day= IDCard.substring(10, 12);
//18位身份证号
}else if(IDCard.length() == EIGHTEEN_ID_CARD){
// 身份证上的年份
year = IDCard.substring(6).substring(0, 4);
// 身份证上的月份
month = IDCard.substring(10).substring(0, 2);
//身份证上的日期
day=IDCard.substring(12).substring(0,2);
}
birthday=year+"年"+month+"月"+day+"日";
}
return birthday;
}
/**
* 身份证验证
* @param id 号码内容
* @return 是否有效
*/
public static boolean isValid(String id){
Boolean validResult = true;
//校验长度只能为15或18
int len = id.length();
if (len != FIFTEEN_ID_CARD && len != EIGHTEEN_ID_CARD){
return false;
}
//校验生日
if (!validDate(id)){
validResult = false;
}
return validResult;
}
/**
* 校验生日
* @param id
* @return
*/
private static boolean validDate(String id)
{
try
{
String birth = id.length() == 15 ? "19" + id.substring(6, 12) : id.substring(6, 14);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date birthDate = sdf.parse(birth);
if (!birth.equals(sdf.format(birthDate))){
return false;
}
}
catch (ParseException e)
{
return false;
}
return true;
}
}