mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix bug and add method
This commit is contained in:
parent
11cf17576a
commit
8683caaad0
@ -9,6 +9,7 @@
|
||||
### 🐣新特性
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 IdcardUtil#getCityCodeByIdCard位数问题(issue#2224@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user