优化代码

dev
ZhouXY108 2024-05-28 09:38:59 +08:00
parent 48bd4f1b31
commit 47eea5fc09
2 changed files with 53 additions and 16 deletions

View File

@ -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");
}
}

View File

@ -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());
}
}