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
aaef00ec
Commit
aaef00ec
authored
Jul 01, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SQL层级处理的支持
parent
73cb3168
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
159 additions
and
4 deletions
+159
-4
TestSfzUtil.java
yzg-util-base/src/test/java/helper/TestSfzUtil.java
+159
-4
No files found.
yzg-util-base/src/test/java/helper/TestSfzUtil.java
View file @
aaef00ec
package
helper
;
import
base.DemoVo
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.SfzhUtil
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.TypeHelper
;
import
com.yanzuoguang.util.helper.*
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
helper.vo.ResponseDataMainResult
;
import
org.junit.Test
;
...
...
@@ -42,4 +39,162 @@ public class TestSfzUtil {
}
return
f
.
getAbsolutePath
();
}
@Test
public
void
testYzg
()
{
validateIdCard18
(
"430524198711048161"
);
validateIdCard18
(
"43052419871104817X"
);
validateIdCard18
(
"441424199203131841"
);
validateIdCard18
(
"44142419920313185X"
);
}
/**
* 验证18位身份编码是否合法
*
* @param idCard 身份编码
* @return 是否合法
*/
public
static
boolean
validateIdCard18
(
String
idCard
)
{
System
.
out
.
println
(
"来源身份证:"
+
idCard
);
boolean
bTrue
=
false
;
if
(
idCard
.
length
()
==
18
)
{
// 前17位
String
code17
=
idCard
.
substring
(
0
,
17
);
System
.
out
.
println
(
"前17位:"
+
code17
);
// 第18位
String
code18
=
idCard
.
substring
(
17
,
18
);
System
.
out
.
println
(
"第18位"
+
code18
);
if
(
isNum
(
code17
))
{
char
[]
cArr
=
code17
.
toCharArray
();
System
.
out
.
println
(
"转换成键盘的位置:"
+
JsonHelper
.
serialize
(
cArr
));
if
(
cArr
!=
null
)
{
int
[]
iCard
=
converCharToInt
(
cArr
);
System
.
out
.
println
(
"转换成整形:"
+
JsonHelper
.
serialize
(
iCard
));
int
iSum17
=
getPowerSum
(
iCard
);
// 获取校验位
String
val
=
getCheckCode18
(
iSum17
);
if
(
val
.
length
()
>
0
&&
val
.
equalsIgnoreCase
(
code18
))
{
bTrue
=
true
;
}
}
}
}
return
bTrue
;
}
/**
* 数字验证
*
* @param val
* @return 提取的数字。
*/
public
static
boolean
isNum
(
String
val
)
{
return
val
==
null
||
""
.
equals
(
val
)
?
false
:
val
.
matches
(
"^[0-9]*$"
);
}
/**
* 将字符数组转换成数字数组
*
* @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
;
System
.
out
.
println
(
"将前17位加上:"
+
JsonHelper
.
serialize
(
power
));
if
(
power
.
length
==
iArr
.
length
)
{
for
(
int
i
=
0
;
i
<
iArr
.
length
;
i
++)
{
int
from
=
iSum
;
iSum
=
iSum
+
iArr
[
i
]
*
power
[
i
];
System
.
out
.
println
(
String
.
format
(
"%d位: %d = %d + %d * %d "
,
i
,
iSum
,
from
,
iArr
[
i
],
power
[
i
]));
}
}
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
;
}
System
.
out
.
println
(
String
.
format
(
"%d %% 11 = %d -> %s"
,
iSum
,
iSum
%
11
,
sCode
));
return
sCode
;
}
/**
* 每位加权因子
*/
public
static
final
int
power
[]
=
{
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
};
}
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