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
f55f4ae6
Commit
f55f4ae6
authored
Jan 24, 2024
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
身份证识别
parent
0d046be0
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
124 additions
and
51 deletions
+124
-51
SfzHandle15.java
...e/src/main/java/com/yanzuoguang/util/sfz/SfzHandle15.java
+23
-13
SfzHandle18.java
...e/src/main/java/com/yanzuoguang/util/sfz/SfzHandle18.java
+16
-19
SfzHandleEn2017.java
...c/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2017.java
+20
-6
SfzHandleEn2023.java
...c/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2023.java
+2
-10
SfzHandleHuZhao.java
...c/main/java/com/yanzuoguang/util/sfz/SfzHandleHuZhao.java
+2
-2
TestSfzUtil.java
yzg-util-base/src/test/java/helper/TestSfzUtil.java
+61
-1
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandle15.java
View file @
f55f4ae6
...
...
@@ -60,20 +60,8 @@ public class SfzHandle15 implements SfzHandle {
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
));
String
sYear
=
getBirthDateYear
(
birthday
);
idCard18
=
idCard
.
substring
(
0
,
6
)
+
sYear
+
idCard
.
substring
(
8
);
// 转换字符数组
char
[]
cArr
=
idCard18
.
toCharArray
();
...
...
@@ -94,6 +82,28 @@ public class SfzHandle15 implements SfzHandle {
return
idCard18
;
}
/**
* 获取生日年份
*
* @param birthday 生日
* @return 年份
*/
public
static
String
getBirthDateYear
(
String
birthday
)
{
// 获取出生年月日
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)
return
String
.
valueOf
(
cal
.
get
(
Calendar
.
YEAR
));
}
@Override
public
String
getGender
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getGender
(
this
.
repair
(
idCard
));
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandle18.java
View file @
f55f4ae6
...
...
@@ -43,19 +43,23 @@ public class SfzHandle18 implements SfzHandle {
String
code17
=
idCard
.
substring
(
0
,
endIndex
);
// 第18位
String
code18
=
idCard
.
substring
(
endIndex
);
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
;
}
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
;
if
(
SfzHandleUtil
.
isNum
(
code17
)
&&
idCard
.
length
()
==
CHINA_ID_MAX_LENGTH
)
{
return
bTrue
;
}
else
if
(
idCard
.
length
()
!=
CHINA_ID_MIN_LENGTH
)
{
return
bTrue
;
}
else
{
return
false
;
}
}
@Override
...
...
@@ -92,14 +96,7 @@ public class SfzHandle18 implements SfzHandle {
@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
;
return
cityCodes
.
get
(
this
.
getAreaId
(
idCard
).
substring
(
0
,
2
));
}
/**
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2017.java
View file @
f55f4ae6
package
com
.
yanzuoguang
.
util
.
sfz
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.regex.Pattern
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandle15
.
CHINA_ID_MIN_LENGTH
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandle15
.
getBirthDateYear
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
cityCodes
;
/**
* 2017年国外永居证处理
*
* @author 颜佐光
*/
public
class
SfzHandleEn2017
implements
SfzHandle
{
public
static
final
Pattern
EN2017
=
Pattern
.
compile
(
"^[a-zA-Z]{3}\\d{12}$"
);
@Override
public
boolean
validate
(
String
idCard
)
{
if
(
idCard
.
length
()
==
CHINA_ID_MIN_LENGTH
&&
EN2017
.
matcher
(
idCard
).
matches
())
{
return
true
;
}
return
false
;
}
@Override
public
String
repair
(
String
idCard
)
{
return
null
;
return
idCard
;
}
@Override
public
String
getGender
(
String
idCard
)
{
return
null
;
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getBirthday
(
String
idCard
)
{
return
null
;
String
birthday
=
idCard
.
substring
(
7
,
13
);
return
getBirthDateYear
(
birthday
)
+
birthday
.
substring
(
2
,
6
);
}
@Override
public
String
getCountry
(
String
idCard
)
{
return
null
;
return
StringHelper
.
EMPTY
;
}
@Override
public
String
getAreaId
(
String
idCard
)
{
return
null
;
return
idCard
.
substring
(
3
,
5
)
;
}
@Override
public
String
getProvinceName
(
String
idCard
)
{
return
null
;
return
cityCodes
.
get
(
this
.
getAreaId
(
idCard
).
substring
(
0
,
2
))
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2023.java
View file @
f55f4ae6
package
com
.
yanzuoguang
.
util
.
sfz
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandle15
.
CHINA_ID_MIN_LENGTH
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandle18
.
CHINA_ID_MAX_LENGTH
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
cityCodes
;
...
...
@@ -32,7 +31,7 @@ public class SfzHandleEn2023 implements SfzHandle {
@Override
public
String
getBirthday
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
get
Gender
(
idCard
);
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
get
Birthday
(
idCard
);
}
@Override
...
...
@@ -47,13 +46,6 @@ public class SfzHandleEn2023 implements SfzHandle {
@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
(
1
,
3
);
}
sProvince
=
cityCodes
.
get
(
sProvinNum
);
return
sProvince
;
return
cityCodes
.
get
(
this
.
getAreaId
(
idCard
).
substring
(
0
,
2
));
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleHuZhao.java
View file @
f55f4ae6
...
...
@@ -11,11 +11,11 @@ import java.util.regex.Pattern;
*/
public
class
SfzHandleHuZhao
implements
SfzHandle
{
private
static
final
String
HU_ZHAO_REGEX
=
"^[a-zA-Z]{0,4}\\d{7,8}$"
;
private
static
final
Pattern
HU_ZHAO_REGEX
=
Pattern
.
compile
(
"^[a-zA-Z]{0,4}\\d{7,8}$"
)
;
@Override
public
boolean
validate
(
String
idCard
)
{
return
Pattern
.
matches
(
HU_ZHAO_REGEX
,
idCard
);
return
HU_ZHAO_REGEX
.
matcher
(
idCard
).
matches
(
);
}
@Override
...
...
yzg-util-base/src/test/java/helper/TestSfzUtil.java
View file @
f55f4ae6
...
...
@@ -5,14 +5,15 @@ import com.yanzuoguang.util.YzgError;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.SfzhUtil
;
import
com.yanzuoguang.util.sfz.SfzHandleUtil
;
import
helper.vo.ResponseDataMainResult
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.io.File
;
import
java.util.List
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
SFZ_HANDLE_15
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
SFZ_HANDLE_18
;
public
class
TestSfzUtil
{
...
...
@@ -55,6 +56,7 @@ public class TestSfzUtil {
validateIdCard18
(
"44142419920313185X"
);
}
@Test
public
void
testId18
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
...
...
@@ -68,6 +70,7 @@ public class TestSfzUtil {
}
}
}
@Test
public
void
testId18AND15
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
...
...
@@ -82,6 +85,7 @@ public class TestSfzUtil {
}
}
}
@Test
public
void
testId18AND15GetAge
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
...
...
@@ -96,6 +100,62 @@ public class TestSfzUtil {
}
}
@Test
public
void
testEn2017
()
throws
NoSuchMethodException
{
String
idCard
=
"PAK310080010103"
;
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_15
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_18
.
validate
(
idCard
));
Assert
.
assertTrue
(
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
validate
(
idCard
));
Assert
.
assertEquals
(
""
,
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
getGender
(
idCard
));
Assert
.
assertEquals
(
"19800101"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
getBirthday
(
idCard
));
Assert
.
assertEquals
(
""
,
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
getCountry
(
idCard
));
Assert
.
assertEquals
(
"31"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
getAreaId
(
idCard
));
Assert
.
assertEquals
(
"上海"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
getProvinceName
(
idCard
));
}
@Test
public
void
testEn2023
()
throws
NoSuchMethodException
{
String
idCard
=
"931586198001010028"
;
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_15
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_18
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
validate
(
idCard
));
Assert
.
assertTrue
(
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
validate
(
idCard
));
Assert
.
assertEquals
(
"女"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
getGender
(
idCard
));
Assert
.
assertEquals
(
"19800101"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
getBirthday
(
idCard
));
Assert
.
assertEquals
(
"586"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
getCountry
(
idCard
));
Assert
.
assertEquals
(
"31"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
getAreaId
(
idCard
));
Assert
.
assertEquals
(
"上海"
,
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
getProvinceName
(
idCard
));
}
@Test
public
void
test18
()
throws
NoSuchMethodException
{
String
idCard
=
"43052419871104817X"
;
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_15
.
validate
(
idCard
));
Assert
.
assertTrue
(
SfzHandleUtil
.
SFZ_HANDLE_18
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
validate
(
idCard
));
Assert
.
assertEquals
(
"男"
,
SfzHandleUtil
.
SFZ_HANDLE_18
.
getGender
(
idCard
));
Assert
.
assertEquals
(
"19871104"
,
SfzHandleUtil
.
SFZ_HANDLE_18
.
getBirthday
(
idCard
));
Assert
.
assertEquals
(
"100"
,
SfzHandleUtil
.
SFZ_HANDLE_18
.
getCountry
(
idCard
));
Assert
.
assertEquals
(
"430524"
,
SfzHandleUtil
.
SFZ_HANDLE_18
.
getAreaId
(
idCard
));
Assert
.
assertEquals
(
"湖南"
,
SfzHandleUtil
.
SFZ_HANDLE_18
.
getProvinceName
(
idCard
));
}
@Test
public
void
test15
()
throws
NoSuchMethodException
{
String
idCard
=
"430524871104817"
;
Assert
.
assertTrue
(
SfzHandleUtil
.
SFZ_HANDLE_15
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_18
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_EN2017
.
validate
(
idCard
));
Assert
.
assertFalse
(
SfzHandleUtil
.
SFZ_HANDLE_EN2023
.
validate
(
idCard
));
Assert
.
assertEquals
(
"男"
,
SfzHandleUtil
.
SFZ_HANDLE_15
.
getGender
(
idCard
));
Assert
.
assertEquals
(
"19871104"
,
SfzHandleUtil
.
SFZ_HANDLE_15
.
getBirthday
(
idCard
));
Assert
.
assertEquals
(
"100"
,
SfzHandleUtil
.
SFZ_HANDLE_15
.
getCountry
(
idCard
));
Assert
.
assertEquals
(
"430524"
,
SfzHandleUtil
.
SFZ_HANDLE_15
.
getAreaId
(
idCard
));
Assert
.
assertEquals
(
"湖南"
,
SfzHandleUtil
.
SFZ_HANDLE_15
.
getProvinceName
(
idCard
));
}
/**
* 验证18位身份编码是否合法
...
...
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