From 47eea5fc092054fee4f28afea9f3f865b731bf37 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 28 May 2024 09:38:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/RandomTools.java | 51 ++++++++++++++++--- .../commons/util/DateTimeToolsTests.java | 18 +++---- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java index ad2a745..fba46f3 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java @@ -1,17 +1,16 @@ package xyz.zhouxy.plusone.commons.util; +import java.security.SecureRandom; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public final class RandomTools { - private RandomTools() { - throw new IllegalStateException("Utility class"); - } + public static final SecureRandom DEFAULT_SECURE_RANDOM = new SecureRandom(); - public static String randomStr(char[] sourceCharacters, int length) { - return randomStr(ThreadLocalRandom.current(), sourceCharacters, length); - } + public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + public static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; + public static final String NUMBERS = "0123456789"; /** * 使用传入的随机数生成器,生成指定长度的字符串 @@ -24,10 +23,48 @@ public final class RandomTools { * @return 随机字符串 */ public static String randomStr(Random random, char[] sourceCharacters, int length) { - char[] result = new char[length]; + final char[] result = new char[length]; for (int i = 0; i < length; i++) { result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)]; } return String.valueOf(result); } + + public static String randomStr(char[] sourceCharacters, int length) { + return randomStr(ThreadLocalRandom.current(), sourceCharacters, length); + } + + public static String secureRandomStr(char[] sourceCharacters, int length) { + return randomStr(DEFAULT_SECURE_RANDOM, sourceCharacters, length); + } + + /** + * 使用传入的随机数生成器,生成指定长度的字符串 + * + * @param random 随机数生成器。根据需要可以传入 + * {@link java.util.concurrent.ThreadLocalRandom}、{@link java.security.SecureRandom} + * 等,不为空 + * @param sourceCharacters 字符池。字符串的字符将在数组中选,不为空 + * @param length 字符串长度 + * @return 随机字符串 + */ + public static String randomStr(Random random, String sourceCharacters, int length) { + final char[] result = new char[length]; + for (int i = 0; i < length; i++) { + result[i] = sourceCharacters.charAt(random.nextInt(sourceCharacters.length())); + } + return String.valueOf(result); + } + + public static String randomStr(String sourceCharacters, int length) { + return randomStr(ThreadLocalRandom.current(), sourceCharacters, length); + } + + public static String secureRandomStr(String sourceCharacters, int length) { + return randomStr(DEFAULT_SECURE_RANDOM, sourceCharacters, length); + } + + private RandomTools() { + throw new IllegalStateException("Utility class"); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java index 27db9fb..912dda6 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java @@ -3,10 +3,7 @@ package xyz.zhouxy.plusone.commons.util; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; + import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,15 +34,18 @@ class DateTimeToolsTests { @Test void testToJodaDateTime() { - DateTime jodaDateTime = DateTimeTools.toJodaDateTime(Instant.now(), ZoneId.of("+08:00")); + Instant now = Instant.now(); + org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("+08:00")); + log.info("jodaDateTime: {}", jodaDateTime); + jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("+02:00")); log.info("jodaDateTime: {}", jodaDateTime); } @Test void test() { - Instant now = Instant.now(); - DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York")); - DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); + java.time.Instant now = java.time.Instant.now(); + org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York")); + org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); log.info(formatter.print(jodaDateTime)); log.info(jodaDateTime.getZone().toString()); log.info(jodaDateTime.toString()); @@ -53,6 +53,6 @@ class DateTimeToolsTests { org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000); log.info(instant.toString()); log.info(DateTimeTools.toJavaInstant(instant).toString()); - log.info(DateTimeTools.toZonedDateTime(instant, DateTimeZone.forID("America/New_York")).toString()); + log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York")).toString()); } }