From 1ee8b792489cbe02d531d39363f7919fe3f3ae83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=BC=9F=E9=BE=99?= Date: Thu, 19 Mar 2020 08:47:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E4=BC=9A=E7=BB=9F=E4=B8=80=E4=BF=A1?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=A0=81=E5=92=8C=E6=95=B0=E5=AD=97=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/lang/PatternPool.java | 18 +++++++++- .../java/cn/hutool/core/lang/Validator.java | 35 +++++++++++++++++++ .../java/cn/hutool/core/math/MathUtil.java | 10 ++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java b/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java index fdf2aeb38..3beaad1bf 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java @@ -1,12 +1,14 @@ package cn.hutool.core.lang; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Pattern; import cn.hutool.core.util.ReUtil; /** * 常用正则表达式集合 - * + * * @author Looly * */ @@ -65,6 +67,20 @@ public class PatternPool { "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]\\d{3}\\d{1,3}[领])|" + "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$"); + + /** 社会统一信用代码 */ + public static final String REGEX = "^[A-Z0-9]{18}$"; + + public static final String BASE_CODE_STRING = "0123456789ABCDEFGHJKLMNPQRTUWXY"; + + public static final String BASE_CODE_REGEX = "[" + BASE_CODE_STRING + "]{18}"; + + public static final List BASE_CODES = new ArrayList<>(); + + public static final int[] WEIGHT = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}; + + public static final char[] BASE_CODE_ARRAY = BASE_CODE_STRING.toCharArray(); + // ------------------------------------------------------------------------------------------------------------------------------------------------------------------- /** Pattern池 */ private static final SimpleCache POOL = new SimpleCache<>(); diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java b/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java index e490bdb48..1023fe04a 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java @@ -1103,4 +1103,39 @@ public class Validator { throw new ValidateException(errorMsg); } } + + /** + * 简单校验统一社会信用代码 + * 18位(大写字母+数字) + * + * @param creditCode 统一社会信用代码 + * @return 校验结果 + */ + public static boolean isCreditCodeBySimple(String creditCode) { + if (StrUtil.isBlank(creditCode)) { + return false; + } + return Pattern.matches(PatternPool.REGEX, creditCode); + } + + /** + * 是否是有效的统一社会信用代码 + * + * @param creditCode 统一社会信用代码 + * @return 校验结果 + */ + public static boolean isCreditCode(String creditCode) { + if (StrUtil.isBlank(creditCode) || !Pattern.matches(PatternPool.BASE_CODE_REGEX, creditCode)) { + return false; + } + char[] businessCodeArray = creditCode.toCharArray(); + char check = businessCodeArray[17]; + int sum = 0, length = 17; + for (int i = 0; i < length; i++) { + char key = businessCodeArray[i]; + sum += (PatternPool.BASE_CODES.indexOf(key) * PatternPool.WEIGHT[i]); + } + int value = 31 - sum % 31; + return check == PatternPool.BASE_CODE_ARRAY[value % 31]; + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/math/MathUtil.java b/hutool-core/src/main/java/cn/hutool/core/math/MathUtil.java index 9fab15912..db3a3bf06 100644 --- a/hutool-core/src/main/java/cn/hutool/core/math/MathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/math/MathUtil.java @@ -76,4 +76,14 @@ public class MathUtil { public static List combinationSelect(String[] datas, int m) { return new Combination(datas).select(m); } + + /** + * 将当前数字转换为几位数字,不足前面补0 + * @param number 要转换的数字 + * @param count 转换为几位数,不足前面补0 + * @return 转换后的数字 + */ + public static String convertNumToDigitString( int number,int count) { + return String.format("%"+count+"d", number).replace(" ", "0");//5代表总共是几位数 + } }