forked from plusone/plusone-commons
优化代码
parent
48bd4f1b31
commit
47eea5fc09
|
@ -1,17 +1,16 @@
|
||||||
package xyz.zhouxy.plusone.commons.util;
|
package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public final class RandomTools {
|
public final class RandomTools {
|
||||||
|
|
||||||
private RandomTools() {
|
public static final SecureRandom DEFAULT_SECURE_RANDOM = new SecureRandom();
|
||||||
throw new IllegalStateException("Utility class");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String randomStr(char[] sourceCharacters, int length) {
|
public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
return randomStr(ThreadLocalRandom.current(), sourceCharacters, length);
|
public static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
|
||||||
}
|
public static final String NUMBERS = "0123456789";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用传入的随机数生成器,生成指定长度的字符串
|
* 使用传入的随机数生成器,生成指定长度的字符串
|
||||||
|
@ -24,10 +23,48 @@ public final class RandomTools {
|
||||||
* @return 随机字符串
|
* @return 随机字符串
|
||||||
*/
|
*/
|
||||||
public static String randomStr(Random random, char[] sourceCharacters, int length) {
|
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++) {
|
for (int i = 0; i < length; i++) {
|
||||||
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
|
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
|
||||||
}
|
}
|
||||||
return String.valueOf(result);
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,7 @@ package xyz.zhouxy.plusone.commons.util;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
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.junit.jupiter.api.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -37,15 +34,18 @@ class DateTimeToolsTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToJodaDateTime() {
|
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);
|
log.info("jodaDateTime: {}", jodaDateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
Instant now = Instant.now();
|
java.time.Instant now = java.time.Instant.now();
|
||||||
DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York"));
|
org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York"));
|
||||||
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
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(formatter.print(jodaDateTime));
|
||||||
log.info(jodaDateTime.getZone().toString());
|
log.info(jodaDateTime.getZone().toString());
|
||||||
log.info(jodaDateTime.toString());
|
log.info(jodaDateTime.toString());
|
||||||
|
@ -53,6 +53,6 @@ class DateTimeToolsTests {
|
||||||
org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000);
|
org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000);
|
||||||
log.info(instant.toString());
|
log.info(instant.toString());
|
||||||
log.info(DateTimeTools.toJavaInstant(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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue