Commit 86d49974 authored by yanzg's avatar yanzg

身份证识别

parent e6c23a21
......@@ -15,7 +15,7 @@ import static com.yanzuoguang.util.sfz.SfzHandleUtil.*;
*/
public class SfzhUtil {
public static final SfzService SERVICE = new SfzService(SFZ_HANDLE_18, SFZ_HANDLE_EN2017, SFZ_HANDLE_EN2023);
public static final SfzService SERVICE = new SfzService(SFZ_HANDLE_18);
/**
* 将15位身份证号码转换为18位
......@@ -175,7 +175,11 @@ public class SfzhUtil {
private static Short getBirthdayShortByIdCard(String idCard, int beginIndex, int endIndex) {
SfzInfo info = SERVICE.getInfo(idCard);
return Short.parseShort(SERVICE.getBirthday(info).substring(beginIndex, endIndex));
String birthday = SERVICE.getBirthday(info);
if (StringHelper.isEmpty(birthday) || birthday.length() < endIndex) {
return 0;
}
return Short.parseShort(birthday.substring(beginIndex, endIndex));
}
/**
......
......@@ -11,6 +11,9 @@ 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 {
public ResponseDataMainResult<List<DemoVo>, DemoVo> getReturnType() {
......@@ -53,17 +56,45 @@ public class TestSfzUtil {
validateIdCard18("44142419920313185X");
}
@Test
public void testId() throws NoSuchMethodException {
public void testId18() throws NoSuchMethodException {
// 获取泛型的返回值类型
String file = getFile("合法身份证.csv");
String s = FileHelper.readFile(new File(file), "utf-8");
SfzhUtil.SERVICE.getHandleList().remove(SFZ_HANDLE_15);
String[] lines = s.split("\\n");
for (String line : lines) {
if (!SfzhUtil.validateCard(line)) {
System.err.println(line);
}
}
}
@Test
public void testId18AND15() throws NoSuchMethodException {
// 获取泛型的返回值类型
String file = getFile("合法身份证.csv");
String s = FileHelper.readFile(new File(file), "utf-8");
String[] lines = s.split("\\n");
// 在程序启动时,将15位当成合法身份证
SfzhUtil.SERVICE.getHandleList().add(SFZ_HANDLE_15);
for (String line : lines) {
if (!SfzhUtil.validateCard(line)) {
System.err.println(line);
}
}
}
@Test
public void testId18AND15GetAge() throws NoSuchMethodException {
// 获取泛型的返回值类型
String file = getFile("合法身份证.csv");
String s = FileHelper.readFile(new File(file), "utf-8");
String[] lines = s.split("\\n");
// 在程序启动时,将15位当成合法身份证
SfzhUtil.SERVICE.getHandleList().add(SFZ_HANDLE_15);
for (String line : lines) {
int ageByIdCard = SfzhUtil.getAgeByIdCard(line);
System.err.println(ageByIdCard);
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment