Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
e6c23a21
Commit
e6c23a21
authored
Jan 23, 2024
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
身份证识别
parent
f11a534b
Changes
13
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
1071 additions
and
481 deletions
+1071
-481
SfzhUtil.java
...e/src/main/java/com/yanzuoguang/util/helper/SfzhUtil.java
+62
-477
SfzHandle.java
...ase/src/main/java/com/yanzuoguang/util/sfz/SfzHandle.java
+62
-0
SfzHandle10.java
...e/src/main/java/com/yanzuoguang/util/sfz/SfzHandle10.java
+207
-0
SfzHandle15.java
...e/src/main/java/com/yanzuoguang/util/sfz/SfzHandle15.java
+157
-0
SfzHandle18.java
...e/src/main/java/com/yanzuoguang/util/sfz/SfzHandle18.java
+180
-0
SfzHandleEn2017.java
...c/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2017.java
+43
-0
SfzHandleEn2023.java
...c/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2023.java
+43
-0
SfzHandleHuZhao.java
...c/main/java/com/yanzuoguang/util/sfz/SfzHandleHuZhao.java
+50
-0
SfzHandleUtil.java
...src/main/java/com/yanzuoguang/util/sfz/SfzHandleUtil.java
+86
-0
SfzInfo.java
...-base/src/main/java/com/yanzuoguang/util/sfz/SfzInfo.java
+32
-0
SfzService.java
...se/src/main/java/com/yanzuoguang/util/sfz/SfzService.java
+131
-0
TestSfzUtil.java
yzg-util-base/src/test/java/helper/TestSfzUtil.java
+16
-4
合法身份证.csv
yzg-util-base/src/test/java/helper/合法身份证.csv
+2
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/SfzhUtil.java
View file @
e6c23a21
This diff is collapsed.
Click to expand it.
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandle.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
/**
* 身份证处理
*
* @author 颜佐光
*/
public
interface
SfzHandle
{
/**
* 是否属于该类型的身份证
*
* @param idCard 是否属于合法身份证
* @return 验证是否完成
*/
boolean
validate
(
String
idCard
);
/**
* 身份证号码修复,修复结果等于原结果,则代表不修复。
*
* @param idCard 身份证号码修复
* @return 修复后的身份证号码
*/
String
repair
(
String
idCard
);
/**
* 获取身份证性别,当没有获取到时,返回空字符串
*
* @param idCard 身份证
* @return 身份证对应的性别,如:男、女
*/
String
getGender
(
String
idCard
);
/**
* 获取身份证对应的生日,当没有获取到时,返回空字符串
*
* @param idCard 身份证
* @return 生日
*/
String
getBirthday
(
String
idCard
);
/**
* 获取国家3位编码
*
* @param idCard 身份证
* @return 国家编号
*/
String
getCountry
(
String
idCard
);
/**
* 获取地区6位编码,不包含国家信息
*
* @param idCard 身份证
* @return 地区编号
*/
String
getAreaId
(
String
idCard
);
/**
* 获取中国的省份
*
* @param idCard 身份证
* @return 地区编号
*/
String
getProvinceName
(
String
idCard
);
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandle10.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 10 个长度的身份证处理,验证身份证是否合法,台湾、香港、澳门
*
* @author 颜佐光
*/
public
class
SfzHandle10
implements
SfzHandle
{
/**
* 台湾身份首字母对应数字
*/
public
static
Map
<
String
,
Integer
>
twFirstCode
=
new
HashMap
<>();
/**
* 香港身份首字母对应数字
*/
public
static
Map
<
String
,
Integer
>
hkFirstCode
=
new
HashMap
<
String
,
Integer
>();
static
{
twFirstCode
.
put
(
"A"
,
10
);
twFirstCode
.
put
(
"B"
,
11
);
twFirstCode
.
put
(
"C"
,
12
);
twFirstCode
.
put
(
"D"
,
13
);
twFirstCode
.
put
(
"E"
,
14
);
twFirstCode
.
put
(
"F"
,
15
);
twFirstCode
.
put
(
"G"
,
16
);
twFirstCode
.
put
(
"H"
,
17
);
twFirstCode
.
put
(
"J"
,
18
);
twFirstCode
.
put
(
"K"
,
19
);
twFirstCode
.
put
(
"L"
,
20
);
twFirstCode
.
put
(
"M"
,
21
);
twFirstCode
.
put
(
"N"
,
22
);
twFirstCode
.
put
(
"P"
,
23
);
twFirstCode
.
put
(
"Q"
,
24
);
twFirstCode
.
put
(
"R"
,
25
);
twFirstCode
.
put
(
"S"
,
26
);
twFirstCode
.
put
(
"T"
,
27
);
twFirstCode
.
put
(
"U"
,
28
);
twFirstCode
.
put
(
"V"
,
29
);
twFirstCode
.
put
(
"X"
,
30
);
twFirstCode
.
put
(
"Y"
,
31
);
twFirstCode
.
put
(
"W"
,
32
);
twFirstCode
.
put
(
"Z"
,
33
);
twFirstCode
.
put
(
"I"
,
34
);
twFirstCode
.
put
(
"O"
,
35
);
hkFirstCode
.
put
(
"A"
,
1
);
hkFirstCode
.
put
(
"B"
,
2
);
hkFirstCode
.
put
(
"C"
,
3
);
hkFirstCode
.
put
(
"R"
,
18
);
hkFirstCode
.
put
(
"U"
,
21
);
hkFirstCode
.
put
(
"Z"
,
26
);
hkFirstCode
.
put
(
"X"
,
24
);
hkFirstCode
.
put
(
"W"
,
23
);
hkFirstCode
.
put
(
"O"
,
15
);
hkFirstCode
.
put
(
"N"
,
14
);
}
@Override
public
boolean
validate
(
String
idCard
)
{
String
card
=
idCard
.
trim
();
String
[]
cardval
=
validateIdCard10
(
card
);
return
cardval
!=
null
&&
cardval
[
2
].
equals
(
"true"
);
}
@Override
public
String
repair
(
String
idCard
)
{
return
idCard
;
}
@Override
public
String
getGender
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
"100"
;
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
/**
* 验证10位身份编码是否合法
*
* @param idCard 身份编码
* @return 身份证信息数组
* <p>
* [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false)
* 若不是身份证件号码则返回null
* </p>
*/
public
static
String
[]
validateIdCard10
(
String
idCard
)
{
String
[]
info
=
new
String
[
3
];
String
card
=
idCard
.
replaceAll
(
"[\\(|\\)]"
,
""
);
if
(
card
.
length
()
!=
8
&&
card
.
length
()
!=
9
&&
idCard
.
length
()
!=
10
)
{
return
null
;
}
if
(
idCard
.
matches
(
"^[a-zA-Z][0-9]{9}$"
))
{
// 台湾
info
[
0
]
=
"台湾"
;
System
.
out
.
println
(
"11111"
);
String
char2
=
idCard
.
substring
(
1
,
2
);
if
(
char2
.
equals
(
"1"
))
{
info
[
1
]
=
"M"
;
System
.
out
.
println
(
"MMMMMMM"
);
}
else
if
(
char2
.
equals
(
"2"
))
{
info
[
1
]
=
"F"
;
System
.
out
.
println
(
"FFFFFFF"
);
}
else
{
info
[
1
]
=
"N"
;
info
[
2
]
=
"false"
;
System
.
out
.
println
(
"NNNN"
);
return
info
;
}
info
[
2
]
=
validateTWCard
(
idCard
)
?
"true"
:
"false"
;
}
else
if
(
idCard
.
matches
(
"^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$"
))
{
// 澳门
info
[
0
]
=
"澳门"
;
info
[
1
]
=
"N"
;
// TODO
}
else
if
(
idCard
.
matches
(
"^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$"
))
{
// 香港
info
[
0
]
=
"香港"
;
info
[
1
]
=
"N"
;
info
[
2
]
=
validateHKCard
(
idCard
)
?
"true"
:
"false"
;
}
else
{
return
null
;
}
return
info
;
}
/**
* 验证台湾身份证号码
*
* @param idCard 身份证号码
* @return 验证码是否符合
*/
public
static
boolean
validateTWCard
(
String
idCard
)
{
String
start
=
idCard
.
substring
(
0
,
1
);
String
mid
=
idCard
.
substring
(
1
,
9
);
String
end
=
idCard
.
substring
(
9
,
10
);
Integer
iStart
=
twFirstCode
.
get
(
start
);
int
sum
=
iStart
/
10
+
(
iStart
%
10
)
*
9
;
char
[]
chars
=
mid
.
toCharArray
();
int
iflag
=
8
;
for
(
char
c
:
chars
)
{
sum
=
sum
+
Integer
.
parseInt
(
c
+
""
)
*
iflag
;
iflag
--;
}
return
(
sum
%
10
==
0
?
0
:
(
10
-
sum
%
10
))
==
Integer
.
parseInt
(
end
);
}
/**
* 验证香港身份证号码(存在Bug,部份特殊身份证无法检查)
* <p>
* 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35
* 最后一位校验码为0-9的数字加上字符"A","A"代表10
* </p>
* <p>
* 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效
* </p>
*
* @param idCard 身份证号码
* @return 验证码是否符合
*/
public
static
boolean
validateHKCard
(
String
idCard
)
{
String
card
=
idCard
.
replaceAll
(
"[\\(|\\)]"
,
""
);
int
sum
=
0
;
int
firstNum
=
Integer
.
valueOf
(
card
.
substring
(
0
,
1
).
toUpperCase
().
toCharArray
()[
0
])
-
55
;
int
secondNum
=
Integer
.
valueOf
(
card
.
substring
(
1
,
2
).
toUpperCase
().
toCharArray
()[
0
])
-
55
;
if
(
card
.
length
()
==
9
)
{
sum
=
firstNum
*
9
+
secondNum
*
8
;
card
=
card
.
substring
(
1
,
9
);
}
else
{
sum
=
522
+
firstNum
*
8
;
}
String
mid
=
card
.
substring
(
1
,
7
);
String
end
=
card
.
substring
(
7
,
8
);
char
[]
chars
=
mid
.
toCharArray
();
int
iflag
=
7
;
for
(
char
c
:
chars
)
{
sum
=
sum
+
Integer
.
parseInt
(
c
+
""
)
*
iflag
;
iflag
--;
}
if
(
end
.
equalsIgnoreCase
(
"A"
))
{
sum
=
sum
+
10
;
}
else
{
sum
=
sum
+
Integer
.
parseInt
(
end
);
}
return
sum
%
11
==
0
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandle15.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Calendar
;
import
java.util.Date
;
/**
* 中国居民15位身份证给处理,验证15位身份编码是否合法
*
* @author 颜佐光
*/
public
class
SfzHandle15
implements
SfzHandle
{
/**
* 中国公民身份证号码最小长度。
*/
public
static
final
int
CHINA_ID_MIN_LENGTH
=
15
;
/**
* 最低年限
*/
public
static
final
int
MIN
=
1930
;
@Override
public
boolean
validate
(
String
idCard
)
{
if
(
idCard
.
length
()
!=
CHINA_ID_MIN_LENGTH
)
{
return
false
;
}
if
(
SfzHandleUtil
.
isNum
(
idCard
))
{
String
proCode
=
idCard
.
substring
(
0
,
2
);
if
(
SfzHandleUtil
.
cityCodes
.
get
(
proCode
)
==
null
)
{
return
false
;
}
String
birthCode
=
idCard
.
substring
(
6
,
12
);
Date
birthDate
=
null
;
try
{
birthDate
=
new
SimpleDateFormat
(
"yy"
).
parse
(
birthCode
.
substring
(
0
,
2
));
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
Calendar
cal
=
Calendar
.
getInstance
();
if
(
birthDate
!=
null
)
cal
.
setTime
(
birthDate
);
return
valiDate
(
cal
.
get
(
Calendar
.
YEAR
),
Integer
.
parseInt
(
birthCode
.
substring
(
2
,
4
)),
Integer
.
parseInt
(
birthCode
.
substring
(
4
,
6
))
);
}
else
{
return
false
;
}
}
@Override
public
String
repair
(
String
idCard
)
{
String
idCard18
=
""
;
if
(
idCard
.
length
()
==
SfzHandle18
.
CHINA_ID_MAX_LENGTH
)
{
return
idCard
;
}
if
(
idCard
.
length
()
!=
CHINA_ID_MIN_LENGTH
)
{
return
null
;
}
if
(
SfzHandleUtil
.
isNum
(
idCard
))
{
// 获取出生年月日
String
birthday
=
idCard
.
substring
(
6
,
12
);
Date
birthDate
=
null
;
try
{
birthDate
=
new
SimpleDateFormat
(
"yyMMdd"
).
parse
(
birthday
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
Calendar
cal
=
Calendar
.
getInstance
();
if
(
birthDate
!=
null
)
{
cal
.
setTime
(
birthDate
);
}
// 获取出生年(完全表现形式,如:2010)
String
sYear
=
String
.
valueOf
(
cal
.
get
(
Calendar
.
YEAR
));
idCard18
=
idCard
.
substring
(
0
,
6
)
+
sYear
+
idCard
.
substring
(
8
);
// 转换字符数组
char
[]
cArr
=
idCard18
.
toCharArray
();
if
(
cArr
!=
null
)
{
int
[]
iCard
=
SfzHandle18
.
converCharToInt
(
cArr
);
int
iSum17
=
SfzHandle18
.
getPowerSum
(
iCard
);
// 获取校验位
String
sVal
=
SfzHandle18
.
getCheckCode18
(
iSum17
);
if
(
sVal
.
length
()
>
0
)
{
idCard18
+=
sVal
;
}
else
{
return
null
;
}
}
}
else
{
return
null
;
}
return
idCard18
;
}
@Override
public
String
getGender
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getGender
(
this
.
repair
(
idCard
));
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getBirthday
(
this
.
repair
(
idCard
));
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getCountry
(
this
.
repair
(
idCard
));
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getAreaId
(
this
.
repair
(
idCard
));
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getProvinceName
(
this
.
repair
(
idCard
));
}
/**
* 验证小于当前日期 是否有效
*
* @param iYear 待验证日期(年)
* @param iMonth 待验证日期(月 1-12)
* @param iDate 待验证日期(日)
* @return 是否有效
*/
public
static
boolean
valiDate
(
int
iYear
,
int
iMonth
,
int
iDate
)
{
Calendar
cal
=
Calendar
.
getInstance
();
int
year
=
cal
.
get
(
Calendar
.
YEAR
);
int
datePerMonth
;
if
(
iYear
<
MIN
||
iYear
>=
year
)
{
return
false
;
}
if
(
iMonth
<
1
||
iMonth
>
12
)
{
return
false
;
}
switch
(
iMonth
)
{
case
4
:
case
6
:
case
9
:
case
11
:
datePerMonth
=
30
;
break
;
case
2
:
boolean
dm
=
((
iYear
%
4
==
0
&&
iYear
%
100
!=
0
)
||
(
iYear
%
400
==
0
))
&&
(
iYear
>
MIN
&&
iYear
<
year
);
datePerMonth
=
dm
?
29
:
28
;
break
;
default
:
datePerMonth
=
31
;
}
return
(
iDate
>=
1
)
&&
(
iDate
<=
datePerMonth
);
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandle18.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandle15
.
CHINA_ID_MIN_LENGTH
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
cityCodes
;
/**
* 18位中国居民身份证处理
*
* @author 颜佐光
*/
public
class
SfzHandle18
implements
SfzHandle
{
/**
* 每位加权因子
*/
public
static
final
int
[]
power
=
{
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
};
/**
* 中国公民身份证号码最大长度。
*/
public
static
final
int
CHINA_ID_MAX_LENGTH
=
18
;
@Override
public
boolean
validate
(
String
idCard
)
{
boolean
bTrue
=
false
;
if
(
idCard
.
length
()
==
CHINA_ID_MAX_LENGTH
)
{
// 前17位
String
code17
=
idCard
.
substring
(
0
,
17
);
// 第18位
String
code18
=
idCard
.
substring
(
17
,
CHINA_ID_MAX_LENGTH
);
if
(
SfzHandleUtil
.
isNum
(
code17
))
{
char
[]
cArr
=
code17
.
toCharArray
();
if
(
cArr
!=
null
)
{
int
[]
iCard
=
converCharToInt
(
cArr
);
int
iSum17
=
getPowerSum
(
iCard
);
// 获取校验位
String
val
=
getCheckCode18
(
iSum17
);
if
(
val
.
length
()
>
0
&&
val
.
equalsIgnoreCase
(
code18
))
{
bTrue
=
true
;
}
}
}
}
return
bTrue
;
}
@Override
public
String
repair
(
String
idCard
)
{
return
idCard
;
}
@Override
public
String
getGender
(
String
idCard
)
{
String
sGender
=
"未知"
;
String
sCardNum
=
idCard
.
substring
(
16
,
17
);
if
(
Integer
.
parseInt
(
sCardNum
)
%
2
!=
0
)
{
sGender
=
"男"
;
}
else
{
sGender
=
"女"
;
}
return
sGender
;
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
idCard
.
substring
(
6
,
14
);
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
"100"
;
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
idCard
.
substring
(
0
,
6
);
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
int
len
=
idCard
.
length
();
String
sProvince
=
null
;
String
sProvinNum
=
""
;
if
(
len
==
CHINA_ID_MIN_LENGTH
||
len
==
CHINA_ID_MAX_LENGTH
)
{
sProvinNum
=
idCard
.
substring
(
0
,
2
);
}
sProvince
=
cityCodes
.
get
(
sProvinNum
);
return
sProvince
;
}
/**
* 将字符数组转换成数字数组
*
* @param ca 字符数组
* @return 数字数组
*/
public
static
int
[]
converCharToInt
(
char
[]
ca
)
{
int
len
=
ca
.
length
;
int
[]
iArr
=
new
int
[
len
];
try
{
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
iArr
[
i
]
=
Integer
.
parseInt
(
String
.
valueOf
(
ca
[
i
]));
}
}
catch
(
NumberFormatException
e
)
{
e
.
printStackTrace
();
}
return
iArr
;
}
/**
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值
*
* @param iArr
* @return 身份证编码。
*/
public
static
int
getPowerSum
(
int
[]
iArr
)
{
int
iSum
=
0
;
if
(
power
.
length
==
iArr
.
length
)
{
for
(
int
i
=
0
;
i
<
iArr
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
power
.
length
;
j
++)
{
if
(
i
==
j
)
{
iSum
=
iSum
+
iArr
[
i
]
*
power
[
j
];
}
}
}
}
return
iSum
;
}
/**
* 将power和值与11取模获得余数进行校验码判断
*
* @param iSum 是否校验位
* @return 校验位
*/
public
static
String
getCheckCode18
(
int
iSum
)
{
String
sCode
=
""
;
switch
(
iSum
%
11
)
{
case
10
:
sCode
=
"2"
;
break
;
case
9
:
sCode
=
"3"
;
break
;
case
8
:
sCode
=
"4"
;
break
;
case
7
:
sCode
=
"5"
;
break
;
case
6
:
sCode
=
"6"
;
break
;
case
5
:
sCode
=
"7"
;
break
;
case
4
:
sCode
=
"8"
;
break
;
case
3
:
sCode
=
"9"
;
break
;
case
2
:
sCode
=
"X"
;
break
;
case
1
:
sCode
=
"0"
;
break
;
case
0
:
sCode
=
"1"
;
break
;
default
:
sCode
=
""
;
break
;
}
return
sCode
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2017.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
/**
* 2017年国外永居证处理
*
* @author 颜佐光
*/
public
class
SfzHandleEn2017
implements
SfzHandle
{
@Override
public
boolean
validate
(
String
idCard
)
{
return
false
;
}
@Override
public
String
repair
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getGender
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
return
null
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2023.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
/**
* 2023年国外永居证处理
*
* @author 颜佐光
*/
public
class
SfzHandleEn2023
implements
SfzHandle
{
@Override
public
boolean
validate
(
String
idCard
)
{
return
false
;
}
@Override
public
String
repair
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getGender
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
null
;
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
return
null
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleHuZhao.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.regex.Pattern
;
/**
* 验证护照是否合法
*
* @author 颜佐光
*/
public
class
SfzHandleHuZhao
implements
SfzHandle
{
private
static
final
String
HU_ZHAO_REGEX
=
"^[a-zA-Z]{0,4}\\d{7,8}$"
;
@Override
public
boolean
validate
(
String
idCard
)
{
return
Pattern
.
matches
(
HU_ZHAO_REGEX
,
idCard
);
}
@Override
public
String
repair
(
String
idCard
)
{
return
idCard
;
}
@Override
public
String
getGender
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
return
null
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleUtil.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 身份证处理工具类
*
* @author 颜佐光
*/
public
class
SfzHandleUtil
{
public
static
Map
<
String
,
String
>
cityCodes
=
new
HashMap
<
String
,
String
>();
/**
* 身份证10处理
*/
public
static
SfzHandle10
SFZ_HANDLE_10
=
new
SfzHandle10
();
/**
* 身份证10处理
*/
public
static
SfzHandle15
SFZ_HANDLE_15
=
new
SfzHandle15
();
/**
* 身份证10处理
*/
public
static
SfzHandle18
SFZ_HANDLE_18
=
new
SfzHandle18
();
/**
* 身份证10处理
*/
public
static
SfzHandleEn2017
SFZ_HANDLE_EN2017
=
new
SfzHandleEn2017
();
/**
* 身份证10处理
*/
public
static
SfzHandleEn2023
SFZ_HANDLE_EN2023
=
new
SfzHandleEn2023
();
/**
* 身份证10处理
*/
public
static
SfzHandleHuZhao
SFZ_HANDLE_HU_ZHAO
=
new
SfzHandleHuZhao
();
static
{
cityCodes
.
put
(
"11"
,
"北京"
);
cityCodes
.
put
(
"12"
,
"天津"
);
cityCodes
.
put
(
"13"
,
"河北"
);
cityCodes
.
put
(
"14"
,
"山西"
);
cityCodes
.
put
(
"15"
,
"内蒙古"
);
cityCodes
.
put
(
"21"
,
"辽宁"
);
cityCodes
.
put
(
"22"
,
"吉林"
);
cityCodes
.
put
(
"23"
,
"黑龙江"
);
cityCodes
.
put
(
"31"
,
"上海"
);
cityCodes
.
put
(
"32"
,
"江苏"
);
cityCodes
.
put
(
"33"
,
"浙江"
);
cityCodes
.
put
(
"34"
,
"安徽"
);
cityCodes
.
put
(
"35"
,
"福建"
);
cityCodes
.
put
(
"36"
,
"江西"
);
cityCodes
.
put
(
"37"
,
"山东"
);
cityCodes
.
put
(
"41"
,
"河南"
);
cityCodes
.
put
(
"42"
,
"湖北"
);
cityCodes
.
put
(
"43"
,
"湖南"
);
cityCodes
.
put
(
"44"
,
"广东"
);
cityCodes
.
put
(
"45"
,
"广西"
);
cityCodes
.
put
(
"46"
,
"海南"
);
cityCodes
.
put
(
"50"
,
"重庆"
);
cityCodes
.
put
(
"51"
,
"四川"
);
cityCodes
.
put
(
"52"
,
"贵州"
);
cityCodes
.
put
(
"53"
,
"云南"
);
cityCodes
.
put
(
"54"
,
"西藏"
);
cityCodes
.
put
(
"61"
,
"陕西"
);
cityCodes
.
put
(
"62"
,
"甘肃"
);
cityCodes
.
put
(
"63"
,
"青海"
);
cityCodes
.
put
(
"64"
,
"宁夏"
);
cityCodes
.
put
(
"65"
,
"新疆"
);
cityCodes
.
put
(
"71"
,
"台湾"
);
cityCodes
.
put
(
"81"
,
"香港"
);
cityCodes
.
put
(
"82"
,
"澳门"
);
cityCodes
.
put
(
"91"
,
"国外"
);
}
/**
* 数字验证
*
* @param val
* @return 提取的数字。
*/
public
static
boolean
isNum
(
String
val
)
{
return
val
!=
null
&&
!
""
.
equals
(
val
)
&&
val
.
matches
(
"^[0-9]*$"
);
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzInfo.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
com.sun.istack.internal.NotNull
;
/**
* 获取身份证信息
*
* @author 颜佐光
*/
public
class
SfzInfo
{
/**
* 身份证编码
*/
private
final
String
cardId
;
/**
* 身份证处理
*/
private
final
SfzHandle
sfzHandle
;
public
SfzInfo
(
@NotNull
String
cardId
,
@NotNull
SfzHandle
sfzHandle
)
{
this
.
cardId
=
cardId
;
this
.
sfzHandle
=
sfzHandle
;
}
public
String
getCardId
()
{
return
cardId
;
}
public
SfzHandle
getSfzHandle
()
{
return
sfzHandle
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzService.java
0 → 100644
View file @
e6c23a21
package
com
.
yanzuoguang
.
util
.
sfz
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Calendar
;
import
java.util.List
;
/**
* 身份证处理服务
*
* @author 颜佐光
*/
public
class
SfzService
{
/**
* 身份证处理类
*/
private
final
List
<
SfzHandle
>
handleList
=
new
ArrayList
<>();
public
SfzService
(
SfzHandle
...
sfzHandles
)
{
this
.
handleList
.
addAll
(
Arrays
.
asList
(
sfzHandles
));
}
/**
* 获取已包含的处理类
*
* @return 已包含的处理类
*/
public
List
<
SfzHandle
>
getHandleList
()
{
return
handleList
;
}
/**
* 获取身份证信息
*
* @param cardId 身份证信息
* @return 返回身份证信息
*/
public
SfzInfo
getInfo
(
String
cardId
)
{
if
(
StringHelper
.
isEmpty
(
cardId
))
{
return
null
;
}
for
(
SfzHandle
sfzHandle
:
handleList
)
{
if
(
sfzHandle
.
validate
(
cardId
))
{
return
new
SfzInfo
(
cardId
,
sfzHandle
);
}
}
return
null
;
}
/**
* 获取修复身份证
*
* @param info 信息
* @return 返回值
*/
public
SfzInfo
repair
(
SfzInfo
info
)
{
if
(
info
==
null
)
{
return
null
;
}
String
repair
=
info
.
getSfzHandle
().
repair
(
info
.
getCardId
());
return
getInfo
(
repair
);
}
/**
* 获取身份证性别,当没有获取到时,返回空字符串
*
* @param info 身份证
* @return 身份证对应的性别, 如:男、女
*/
public
String
getGender
(
SfzInfo
info
)
{
if
(
info
==
null
)
{
return
StringHelper
.
EMPTY
;
}
return
info
.
getSfzHandle
().
getGender
(
info
.
getCardId
());
}
/**
* 获取身份证对应的生日,当没有获取到时,返回空字符串
*
* @param info 身份证
* @return 生日
*/
public
String
getBirthday
(
SfzInfo
info
)
{
if
(
info
==
null
)
{
return
StringHelper
.
EMPTY
;
}
return
info
.
getSfzHandle
().
getBirthday
(
info
.
getCardId
());
}
/**
* 获取国家3位编码
*
* @param info 身份证
* @return 国家编号
*/
public
String
getCountry
(
SfzInfo
info
)
{
if
(
info
==
null
)
{
return
StringHelper
.
EMPTY
;
}
return
info
.
getSfzHandle
().
getCountry
(
info
.
getCardId
());
}
/**
* 获取地区6位编码,不包含国家信息
*
* @param info 身份证
* @return 地区编号
*/
public
String
getAreaId
(
SfzInfo
info
)
{
if
(
info
==
null
)
{
return
StringHelper
.
EMPTY
;
}
return
info
.
getSfzHandle
().
getAreaId
(
info
.
getCardId
());
}
/**
* 获取省份名称
*
* @param info 身份证
* @return 地区编号
*/
public
String
getProvinceName
(
SfzInfo
info
)
{
if
(
info
==
null
)
{
return
StringHelper
.
EMPTY
;
}
return
info
.
getSfzHandle
().
getProvinceName
(
info
.
getCardId
());
}
}
yzg-util-base/src/test/java/helper/TestSfzUtil.java
View file @
e6c23a21
...
...
@@ -2,13 +2,13 @@ package helper;
import
base.DemoVo
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.helper.*
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.SfzhUtil
;
import
helper.vo.ResponseDataMainResult
;
import
org.junit.Test
;
import
java.io.File
;
import
java.lang.reflect.Method
;
import
java.util.List
;
public
class
TestSfzUtil
{
...
...
@@ -25,7 +25,7 @@ public class TestSfzUtil {
String
[]
lines
=
s
.
split
(
"\\n"
);
for
(
String
line
:
lines
)
{
if
(!
SfzhUtil
.
validateCard
(
line
))
{
System
.
out
.
println
(
line
);
System
.
err
.
println
(
line
);
}
}
}
...
...
@@ -52,6 +52,18 @@ public class TestSfzUtil {
validateIdCard18
(
"44142419920313185X"
);
}
@Test
public
void
testId
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
String
file
=
getFile
(
"合法身份证.csv"
);
String
s
=
FileHelper
.
readFile
(
new
File
(
file
),
"utf-8"
);
String
[]
lines
=
s
.
split
(
"\\n"
);
for
(
String
line
:
lines
)
{
if
(!
SfzhUtil
.
validateCard
(
line
))
{
System
.
err
.
println
(
line
);
}
}
}
/**
...
...
yzg-util-base/src/test/java/helper/合法身份证.csv
0 → 100644
View file @
e6c23a21
43052419871104817X
430524871104817
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment