diff --git a/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java index c8be84070c..c2495212fd 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java @@ -278,8 +278,8 @@ public static boolean isValidCard18(String idcard, boolean ignoreCase) { return false; } - // 省份 - final String proCode = idcard.substring(0, 2); + // 截取省份代码。新版外国人永久居留身份证以9开头,第二三位是受理地代码 + final String proCode = idcard.startsWith("9") ? idcard.substring(1, 3): idcard.substring(0, 2); if (null == CITY_CODES.get(proCode)) { return false; } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java index 97c7b117a9..736bb4fcb3 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java @@ -14,6 +14,10 @@ public class IdcardUtilTest { private static final String ID_18 = "321083197812162119"; + /** + * 新版外国人永久居留身份证号码 + */ + private static final String FOREIGN_ID_18 = "932682198501010017"; private static final String ID_15 = "150102880730303"; @Test @@ -24,6 +28,8 @@ public void isValidCardTest() { boolean valid15 = IdcardUtil.isValidCard(ID_15); Assert.assertTrue(valid15); + Assert.assertTrue(IdcardUtil.isValidCard(FOREIGN_ID_18)); + // 无效 String idCard = "360198910283844"; Assert.assertFalse(IdcardUtil.isValidCard(idCard)); @@ -58,6 +64,7 @@ public void getAgeByIdCardTest() { int age = IdcardUtil.getAgeByIdCard(ID_18, date); Assert.assertEquals(age, 38); + Assert.assertEquals(IdcardUtil.getAgeByIdCard(FOREIGN_ID_18, date), 32); int age2 = IdcardUtil.getAgeByIdCard(ID_15, date); Assert.assertEquals(age2, 28); @@ -127,6 +134,10 @@ public void isValidCard18Test(){ // 台湾人在大陆身份证 isValidCard18 = IdcardUtil.isValidCard18("830000200209060065"); Assert.assertTrue(isValidCard18); + + // 新版外国人永久居留身份证 + isValidCard18 = IdcardUtil.isValidCard18("932682198501010017"); + Assert.assertTrue(isValidCard18); } @Test