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
Show 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 {
...
@@ -60,20 +60,8 @@ public class SfzHandle15 implements SfzHandle {
return
null
;
return
null
;
}
}
if
(
SfzHandleUtil
.
isNum
(
idCard
))
{
if
(
SfzHandleUtil
.
isNum
(
idCard
))
{
// 获取出生年月日
String
birthday
=
idCard
.
substring
(
6
,
12
);
String
birthday
=
idCard
.
substring
(
6
,
12
);
Date
birthDate
=
null
;
String
sYear
=
getBirthDateYear
(
birthday
);
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
);
idCard18
=
idCard
.
substring
(
0
,
6
)
+
sYear
+
idCard
.
substring
(
8
);
// 转换字符数组
// 转换字符数组
char
[]
cArr
=
idCard18
.
toCharArray
();
char
[]
cArr
=
idCard18
.
toCharArray
();
...
@@ -94,6 +82,28 @@ public class SfzHandle15 implements SfzHandle {
...
@@ -94,6 +82,28 @@ public class SfzHandle15 implements SfzHandle {
return
idCard18
;
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
@Override
public
String
getGender
(
String
idCard
)
{
public
String
getGender
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
getGender
(
this
.
repair
(
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,7 +43,6 @@ public class SfzHandle18 implements SfzHandle {
...
@@ -43,7 +43,6 @@ public class SfzHandle18 implements SfzHandle {
String
code17
=
idCard
.
substring
(
0
,
endIndex
);
String
code17
=
idCard
.
substring
(
0
,
endIndex
);
// 第18位
// 第18位
String
code18
=
idCard
.
substring
(
endIndex
);
String
code18
=
idCard
.
substring
(
endIndex
);
if
(
SfzHandleUtil
.
isNum
(
code17
))
{
char
[]
cArr
=
code17
.
toCharArray
();
char
[]
cArr
=
code17
.
toCharArray
();
if
(
cArr
!=
null
)
{
if
(
cArr
!=
null
)
{
int
[]
iCard
=
converCharToInt
(
cArr
);
int
[]
iCard
=
converCharToInt
(
cArr
);
...
@@ -54,8 +53,13 @@ public class SfzHandle18 implements SfzHandle {
...
@@ -54,8 +53,13 @@ public class SfzHandle18 implements SfzHandle {
bTrue
=
true
;
bTrue
=
true
;
}
}
}
}
}
if
(
SfzHandleUtil
.
isNum
(
code17
)
&&
idCard
.
length
()
==
CHINA_ID_MAX_LENGTH
)
{
return
bTrue
;
}
else
if
(
idCard
.
length
()
!=
CHINA_ID_MIN_LENGTH
)
{
return
bTrue
;
return
bTrue
;
}
else
{
return
false
;
}
}
}
@Override
@Override
...
@@ -92,14 +96,7 @@ public class SfzHandle18 implements SfzHandle {
...
@@ -92,14 +96,7 @@ public class SfzHandle18 implements SfzHandle {
@Override
@Override
public
String
getProvinceName
(
String
idCard
)
{
public
String
getProvinceName
(
String
idCard
)
{
int
len
=
idCard
.
length
();
return
cityCodes
.
get
(
this
.
getAreaId
(
idCard
).
substring
(
0
,
2
));
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
;
}
}
/**
/**
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleEn2017.java
View file @
f55f4ae6
package
com
.
yanzuoguang
.
util
.
sfz
;
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年国外永居证处理
* 2017年国外永居证处理
*
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
SfzHandleEn2017
implements
SfzHandle
{
public
class
SfzHandleEn2017
implements
SfzHandle
{
public
static
final
Pattern
EN2017
=
Pattern
.
compile
(
"^[a-zA-Z]{3}\\d{12}$"
);
@Override
@Override
public
boolean
validate
(
String
idCard
)
{
public
boolean
validate
(
String
idCard
)
{
if
(
idCard
.
length
()
==
CHINA_ID_MIN_LENGTH
&&
EN2017
.
matcher
(
idCard
).
matches
())
{
return
true
;
}
return
false
;
return
false
;
}
}
@Override
@Override
public
String
repair
(
String
idCard
)
{
public
String
repair
(
String
idCard
)
{
return
null
;
return
idCard
;
}
}
@Override
@Override
public
String
getGender
(
String
idCard
)
{
public
String
getGender
(
String
idCard
)
{
return
null
;
return
StringHelper
.
EMPTY
;
}
}
@Override
@Override
public
String
getBirthday
(
String
idCard
)
{
public
String
getBirthday
(
String
idCard
)
{
return
null
;
String
birthday
=
idCard
.
substring
(
7
,
13
);
return
getBirthDateYear
(
birthday
)
+
birthday
.
substring
(
2
,
6
);
}
}
@Override
@Override
public
String
getCountry
(
String
idCard
)
{
public
String
getCountry
(
String
idCard
)
{
return
null
;
return
StringHelper
.
EMPTY
;
}
}
@Override
@Override
public
String
getAreaId
(
String
idCard
)
{
public
String
getAreaId
(
String
idCard
)
{
return
null
;
return
idCard
.
substring
(
3
,
5
)
;
}
}
@Override
@Override
public
String
getProvinceName
(
String
idCard
)
{
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
;
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
.
SfzHandle18
.
CHINA_ID_MAX_LENGTH
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
cityCodes
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
cityCodes
;
...
@@ -32,7 +31,7 @@ public class SfzHandleEn2023 implements SfzHandle {
...
@@ -32,7 +31,7 @@ public class SfzHandleEn2023 implements SfzHandle {
@Override
@Override
public
String
getBirthday
(
String
idCard
)
{
public
String
getBirthday
(
String
idCard
)
{
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
get
Gender
(
idCard
);
return
SfzHandleUtil
.
SFZ_HANDLE_18
.
get
Birthday
(
idCard
);
}
}
@Override
@Override
...
@@ -47,13 +46,6 @@ public class SfzHandleEn2023 implements SfzHandle {
...
@@ -47,13 +46,6 @@ public class SfzHandleEn2023 implements SfzHandle {
@Override
@Override
public
String
getProvinceName
(
String
idCard
)
{
public
String
getProvinceName
(
String
idCard
)
{
int
len
=
idCard
.
length
();
return
cityCodes
.
get
(
this
.
getAreaId
(
idCard
).
substring
(
0
,
2
));
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
;
}
}
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/sfz/SfzHandleHuZhao.java
View file @
f55f4ae6
...
@@ -11,11 +11,11 @@ import java.util.regex.Pattern;
...
@@ -11,11 +11,11 @@ import java.util.regex.Pattern;
*/
*/
public
class
SfzHandleHuZhao
implements
SfzHandle
{
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
@Override
public
boolean
validate
(
String
idCard
)
{
public
boolean
validate
(
String
idCard
)
{
return
Pattern
.
matches
(
HU_ZHAO_REGEX
,
idCard
);
return
HU_ZHAO_REGEX
.
matcher
(
idCard
).
matches
(
);
}
}
@Override
@Override
...
...
yzg-util-base/src/test/java/helper/TestSfzUtil.java
View file @
f55f4ae6
...
@@ -5,14 +5,15 @@ import com.yanzuoguang.util.YzgError;
...
@@ -5,14 +5,15 @@ import com.yanzuoguang.util.YzgError;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.SfzhUtil
;
import
com.yanzuoguang.util.helper.SfzhUtil
;
import
com.yanzuoguang.util.sfz.SfzHandleUtil
;
import
helper.vo.ResponseDataMainResult
;
import
helper.vo.ResponseDataMainResult
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.io.File
;
import
java.io.File
;
import
java.util.List
;
import
java.util.List
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
SFZ_HANDLE_15
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
SFZ_HANDLE_15
;
import
static
com
.
yanzuoguang
.
util
.
sfz
.
SfzHandleUtil
.
SFZ_HANDLE_18
;
public
class
TestSfzUtil
{
public
class
TestSfzUtil
{
...
@@ -55,6 +56,7 @@ public class TestSfzUtil {
...
@@ -55,6 +56,7 @@ public class TestSfzUtil {
validateIdCard18
(
"44142419920313185X"
);
validateIdCard18
(
"44142419920313185X"
);
}
}
@Test
@Test
public
void
testId18
()
throws
NoSuchMethodException
{
public
void
testId18
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
// 获取泛型的返回值类型
...
@@ -68,6 +70,7 @@ public class TestSfzUtil {
...
@@ -68,6 +70,7 @@ public class TestSfzUtil {
}
}
}
}
}
}
@Test
@Test
public
void
testId18AND15
()
throws
NoSuchMethodException
{
public
void
testId18AND15
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
// 获取泛型的返回值类型
...
@@ -82,6 +85,7 @@ public class TestSfzUtil {
...
@@ -82,6 +85,7 @@ public class TestSfzUtil {
}
}
}
}
}
}
@Test
@Test
public
void
testId18AND15GetAge
()
throws
NoSuchMethodException
{
public
void
testId18AND15GetAge
()
throws
NoSuchMethodException
{
// 获取泛型的返回值类型
// 获取泛型的返回值类型
...
@@ -96,6 +100,62 @@ public class TestSfzUtil {
...
@@ -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位身份编码是否合法
* 验证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