fix bug and add method

This commit is contained in:
Looly 2022-03-28 15:57:36 +08:00
parent 11cf17576a
commit 8683caaad0
3 changed files with 28 additions and 4 deletions

View File

@ -9,6 +9,7 @@
### 🐣新特性
### 🐞Bug修复
* 【core 】 IdcardUtil#getCityCodeByIdCard位数问题issue#2224@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -580,15 +580,32 @@ public class IdcardUtil {
}
/**
* 根据身份编号获取市级编码只支持15或18位身份证号码
* 根据身份编号获取地市级编码只支持15或18位身份证号码<br>
* 获取编码为4位
*
* @param idcard 身份编码
* @return 市级编码
* @return 市级编码
*/
public static String getCityCodeByIdCard(String idcard) {
int len = idcard.length();
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
return idcard.substring(0, 5);
return idcard.substring(0, 4);
}
return null;
}
/**
* 根据身份编号获取区县级编码只支持15或18位身份证号码<br>
* 获取编码为6位
*
* @param idcard 身份编码
* @return 地市级编码
* @since 5.8.0
*/
public static String getDistrictCodeByIdCard(String idcard) {
int len = idcard.length();
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
return idcard.substring(0, 6);
}
return null;
}

View File

@ -78,7 +78,13 @@ public class IdcardUtilTest {
@Test
public void getCityCodeByIdCardTest() {
String codeByIdCard = IdcardUtil.getCityCodeByIdCard(ID_18);
Assert.assertEquals("32108", codeByIdCard);
Assert.assertEquals("3210", codeByIdCard);
}
@Test
public void getDistrictCodeByIdCardTest() {
String codeByIdCard = IdcardUtil.getDistrictCodeByIdCard(ID_18);
Assert.assertEquals("321083", codeByIdCard);
}
@Test