From b8977ea1192f0216c8b4068e9569a35753ebd2fb Mon Sep 17 00:00:00 2001 From: hellozrh <455741807@qq.com> Date: Tue, 9 Aug 2022 15:20:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Validator=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=A4=E4=B8=AA=E9=AA=8C=E8=AF=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/lang/Validator.java | 38 +++++++++++++++++++ .../cn/hutool/core/lang/ValidatorTest.java | 25 ++++++++++++ 2 files changed, 63 insertions(+) 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 368acb9a7..43fbd1968 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java @@ -11,6 +11,7 @@ import cn.hutool.core.text.StrUtil; import cn.hutool.core.util.IdcardUtil; import java.net.MalformedURLException; +import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1046,6 +1047,29 @@ public class Validator { } } + /** + * 检查给定的日期是否在指定范围内 + * + * @param value 值 + * @param start 最小值(包含) + * @param end 最大值(包含) + * @param errorMsg 验证错误的信息 + * @throws ValidateException 验证异常 + * @author ZRH 455741807@qq.com + */ + public static void validateBetween(final Date value, final Date start, final Date end, final String errorMsg) { + Assert.notNull(value); + Assert.notNull(start); + Assert.notNull(end); + int c1 = DateUtil.compare(value, start); + int c2 = DateUtil.compare(value, end); + + if(c1 >= 0 && c2 <= 0){ + return; + } + throw new ValidateException(errorMsg); + } + /** * 是否是有效的统一社会信用代码 *
@@ -1160,4 +1184,18 @@ public class Validator {
 		}
 		return value;
 	}
+
+	/**
+	 * 验证字符的长度是否符合要求
+	 * @param str      字符串
+	 * @param min      最小长度
+	 * @param max      最大长度
+	 * @param errorMsg 错误消息
+	 */
+	public static void validateLength(CharSequence str, int min, int max, String errorMsg) {
+		int len = StrUtil.length(str);
+		if (len < min || len > max) {
+			throw new ValidateException(errorMsg);
+		}
+	}
 }
diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java
index 75fc1b53d..1148bb512 100755
--- a/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java
@@ -1,11 +1,14 @@
 package cn.hutool.core.lang;
 
+import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.exceptions.ValidateException;
 import cn.hutool.core.lang.id.IdUtil;
 import cn.hutool.core.regex.PatternPool;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.util.Date;
+
 /**
  * 验证器单元测试
  *
@@ -248,6 +251,28 @@ public class ValidatorTest {
 		Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, content));
 	}
 
+	@Test
+	public void validateBetweenTest(){
+		Date value = DateUtil.parse("2022-08-09 21:22:00");
+		final Date start = DateUtil.parse("2022-08-01 21:22:00");
+		final Date end = DateUtil.parse("2022-09-09 21:22:00");
+
+		Validator.validateBetween(value, start, end, "您选择的日期超出规定范围!");
+		Assert.assertTrue(true);
+
+		final Date value2 = DateUtil.parse("2023-08-09 21:22:00");
+		Assert.assertThrows(ValidateException.class, ()->
+				Validator.validateBetween(value2, start, end, "您选择的日期超出规定范围!")
+		);
+	}
+
+	@Test
+	public void validateLengthTest() {
+		String s1 = "abc";
+		Assert.assertThrows(ValidateException.class, ()->
+				Validator.validateLength(s1, 6, 8, "请输入6到8位的字符!"));
+	}
+
 	@Test
 	public void isChineseNameTest(){
 		Assert.assertTrue(Validator.isChineseName("阿卜杜尼亚孜·毛力尼亚孜"));

From ad09bd41bb41cfde5a99d2dddcc9d8c97c1a81ef Mon Sep 17 00:00:00 2001
From: hellozrh <455741807@qq.com>
Date: Tue, 9 Aug 2022 15:30:52 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=97=E8=8A=82?=
 =?UTF-8?q?=E9=95=BF=E5=BA=A6=E9=AA=8C=E8=AF=81=EF=BC=8C=E5=BA=94=E5=AF=B9?=
 =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AD=97=E8=8A=82=E9=95=BF=E5=BA=A6?=
 =?UTF-8?q?=E4=B8=8Ejava=E4=B8=B2=E9=95=BF=E5=BA=A6=E7=AE=97=E6=B3=95?=
 =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E7=9A=84=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../java/cn/hutool/core/lang/Validator.java   | 30 +++++++++++++++++++
 .../cn/hutool/core/lang/ValidatorTest.java    | 18 +++++++++++
 2 files changed, 48 insertions(+)

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 43fbd1968..850ce1cdb 100755
--- a/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java
+++ b/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java
@@ -3,6 +3,7 @@ package cn.hutool.core.lang;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.exceptions.ValidateException;
 import cn.hutool.core.regex.PatternPool;
+import cn.hutool.core.util.CharsetUtil;
 import cn.hutool.core.util.CreditCodeUtil;
 import cn.hutool.core.math.NumberUtil;
 import cn.hutool.core.util.ObjUtil;
@@ -11,6 +12,7 @@ import cn.hutool.core.text.StrUtil;
 import cn.hutool.core.util.IdcardUtil;
 
 import java.net.MalformedURLException;
+import java.nio.charset.Charset;
 import java.util.Date;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -1198,4 +1200,32 @@ public class Validator {
 			throw new ValidateException(errorMsg);
 		}
 	}
+
+	/**
+	 * 验证字符串的字节长度是否符合要求,默认采用"utf-8"编码
+	 *
+	 * @param str      字符串
+	 * @param min      最小长度
+	 * @param max      最大长度
+	 * @param errorMsg 错误消息
+	 */
+	public static void validateByteLength(CharSequence str, int min, int max, String errorMsg) {
+		validateByteLength(str, min, max, CharsetUtil.UTF_8, errorMsg);
+	}
+
+	/**
+	 * 验证字符串的字节长度是否符合要求
+	 *
+	 * @param str      字符串
+	 * @param min      最小长度
+	 * @param max      最大长度
+	 * @param charset  字符编码
+	 * @param errorMsg 错误消息
+	 */
+	public static void validateByteLength(CharSequence str, int min, int max, Charset charset, String errorMsg) {
+		int len = StrUtil.byteLength(str, charset);
+		if (len < min || len > max) {
+			throw new ValidateException(errorMsg);
+		}
+	}
 }
diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java
index 1148bb512..dab979e0a 100755
--- a/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java
@@ -4,6 +4,8 @@ import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.exceptions.ValidateException;
 import cn.hutool.core.lang.id.IdUtil;
 import cn.hutool.core.regex.PatternPool;
+import cn.hutool.core.text.StrUtil;
+import cn.hutool.core.util.CharsetUtil;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -273,6 +275,22 @@ public class ValidatorTest {
 				Validator.validateLength(s1, 6, 8, "请输入6到8位的字符!"));
 	}
 
+	@Test
+	public void validateByteLengthTest() {
+		String s1 = "abc";
+		int len1 = StrUtil.byteLength(s1, CharsetUtil.UTF_8);
+		Assert.assertEquals(len1, 3);
+
+		String s2 = "我ab";
+		int len2 = StrUtil.byteLength(s2, CharsetUtil.UTF_8);
+		Assert.assertEquals(len2, 5);
+
+		//一个汉字在utf-8编码下,占3个字节。
+		Assert.assertThrows(ValidateException.class, ()->
+				Validator.validateByteLength(s2, 1, 3, "您输入的字符串长度超出限制!")
+		);
+	}
+
 	@Test
 	public void isChineseNameTest(){
 		Assert.assertTrue(Validator.isChineseName("阿卜杜尼亚孜·毛力尼亚孜"));