Skip to content

Commit

Permalink
Merge pull request #49 from yindz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yindz authored Apr 23, 2022
2 parents 9bd9af3 + 0af1895 commit 64a347c
Show file tree
Hide file tree
Showing 4 changed files with 687 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.apifan.common</groupId>
<artifactId>common-random</artifactId>
<version>1.0.12</version>
<version>1.0.13</version>
<packaging>jar</packaging>
<name>common-random</name>
<description>An easy-to-use random data generator.</description>
Expand All @@ -22,7 +22,7 @@
<commons-io.version>2.7</commons-io.version>
<commons-collections.version>4.1</commons-collections.version>
<guava.version>30.0-jre</guava.version>
<jackson-databind.version>2.10.0.pr1</jackson-databind.version>
<jackson-databind.version>2.12.6.1</jackson-databind.version>
<tinypinyin.version>2.0.3.RELEASE</tinypinyin.version>
<slf4j-api.version>1.7.29</slf4j-api.version>
<slf4j-simple.version>1.7.29</slf4j-simple.version>
Expand Down
207 changes: 198 additions & 9 deletions src/main/java/com/apifan/common/random/source/DateTimeSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -55,30 +56,72 @@ public static DateTimeSource getInstance() {
*/
public String randomDate(int year, String pattern) {
Preconditions.checkArgument(StringUtils.isNotEmpty(pattern), "日期格式为空");
LocalDate date = randomLocalDate(year);
return date.format(dateTimeFormatterMap.computeIfAbsent(pattern, k -> DateTimeFormatter.ofPattern(pattern)));
}

/**
* 随机日期
*
* @param year 年份
* @return 随机日期
*/
public Date randomDate(int year) {
return localDateToDate(randomLocalDate(year));
}

/**
* 随机的LocalDate日期
*
* @param year 年份
* @return 随机的LocalDate日期
*/
public LocalDate randomLocalDate(int year) {
Preconditions.checkArgument(year >= 1970 && year <= 9999, "年份无效");
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
LocalDate begin = LocalDate.of(year, 1, 1);
LocalDate date = begin.plusDays(RandomUtils.nextInt(0, isLeapYear ? 366 : 365));
return date.format(dateTimeFormatterMap.computeIfAbsent(pattern, k -> DateTimeFormatter.ofPattern(pattern)));
return begin.plusDays(RandomUtils.nextInt(0, isLeapYear ? 366 : 365));
}

/**
* 获取特定范围内的随机日期
*
* @param beginDate 范围开始(含)
* @param endDate 范围结束(含)
* @param pattern 日期格式
* @return 随机日期字符串
*/
public String randomDate(LocalDate beginDate, LocalDate endDate, String pattern) {
public LocalDate randomLocalDate(LocalDate beginDate, LocalDate endDate) {
Preconditions.checkArgument(beginDate != null && endDate != null, "日期范围不能为空");
Preconditions.checkArgument(beginDate.isBefore(endDate), "日期范围无效");
Preconditions.checkArgument(StringUtils.isNotEmpty(pattern), "日期格式为空");
long diff = DAYS.between(beginDate, endDate);
LocalDate date = beginDate.plusDays(RandomUtils.nextLong(0, diff + 1));
return beginDate.plusDays(RandomUtils.nextLong(0, diff + 1));
}

/**
* 获取特定范围内的随机日期
*
* @param beginDate 范围开始(含)
* @param endDate 范围结束(含)
* @param pattern 日期格式
* @return 随机日期字符串
*/
public String randomDate(LocalDate beginDate, LocalDate endDate, String pattern) {
Preconditions.checkArgument(StringUtils.isNotEmpty(pattern), "日期格式为空");
LocalDate date = randomLocalDate(beginDate, endDate);
return date.format(dateTimeFormatterMap.computeIfAbsent(pattern, k -> DateTimeFormatter.ofPattern(pattern)));
}

/**
* 获取特定范围内的随机日期
*
* @param beginDate 范围开始(含)
* @param endDate 范围结束(含)
* @return 随机日期
*/
public Date randomDate(LocalDate beginDate, LocalDate endDate) {
return localDateToDate(randomLocalDate(beginDate, endDate));
}

/**
* 随机未来日期
*
Expand All @@ -88,10 +131,30 @@ public String randomDate(LocalDate beginDate, LocalDate endDate, String pattern)
*/
public String randomFutureDate(LocalDate baseDate, String pattern) {
Preconditions.checkArgument(StringUtils.isNotEmpty(pattern), "日期格式为空");
LocalDate date = baseDate.plusDays(RandomUtils.nextLong(1, 99999));
LocalDate date = randomFutureLocalDate(baseDate);
return date.format(dateTimeFormatterMap.computeIfAbsent(pattern, k -> DateTimeFormatter.ofPattern(pattern)));
}

/**
* 随机未来日期
*
* @param baseDate 基础日期
* @return 随机未来日期
*/
public LocalDate randomFutureLocalDate(LocalDate baseDate) {
return baseDate.plusDays(RandomUtils.nextLong(1, 99999));
}

/**
* 随机未来日期
*
* @param baseDate 基础日期
* @return 随机未来日期
*/
public Date randomFutureDate(LocalDate baseDate) {
return localDateToDate(randomFutureLocalDate(baseDate));
}

/**
* 随机未来日期(以当天为基准)
*
Expand All @@ -102,6 +165,15 @@ public String randomFutureDate(String pattern) {
return randomFutureDate(LocalDate.now(), pattern);
}

/**
* 随机未来日期(以当天为基准)
*
* @return 随机未来日期字符串
*/
public Date randomFutureDate() {
return localDateToDate(randomFutureLocalDate(LocalDate.now()));
}

/**
* 随机以往日期
*
Expand All @@ -112,11 +184,33 @@ public String randomFutureDate(String pattern) {
*/
public String randomPastDate(LocalDate baseDate, long maxDays, String pattern) {
Preconditions.checkArgument(StringUtils.isNotEmpty(pattern), "日期格式为空");
Preconditions.checkArgument(maxDays > 1, "最大日期间隔无效");
LocalDate date = baseDate.plusDays(-1 * RandomUtils.nextLong(1, maxDays + 1));
LocalDate date = randomPastLocalDate(baseDate, maxDays);
return date.format(dateTimeFormatterMap.computeIfAbsent(pattern, k -> DateTimeFormatter.ofPattern(pattern)));
}

/**
* 随机以往日期
*
* @param baseDate 基础日期
* @param maxDays 最大日期间隔(天)
* @return 随机以往日期
*/
public LocalDate randomPastLocalDate(LocalDate baseDate, long maxDays) {
Preconditions.checkArgument(maxDays > 1, "最大日期间隔无效");
return baseDate.plusDays(-1 * RandomUtils.nextLong(1, maxDays + 1));
}

/**
* 随机以往日期
*
* @param baseDate 基础日期
* @param maxDays 最大日期间隔(天)
* @return 随机以往日期
*/
public Date randomPastDate(LocalDate baseDate, long maxDays) {
return localDateToDate(randomPastLocalDate(baseDate, maxDays));
}

/**
* 随机以往日期(1年内)
*
Expand All @@ -128,6 +222,16 @@ public String randomPastDate(LocalDate baseDate, String pattern) {
return randomPastDate(baseDate, 365, pattern);
}

/**
* 随机以往日期(1年内)
*
* @param baseDate 基础日期
* @return 随机以往日期
*/
public Date randomPastDate(LocalDate baseDate) {
return localDateToDate(randomPastLocalDate(baseDate, 365));
}

/**
* 随机以往日期(1年内)
*
Expand All @@ -138,6 +242,15 @@ public String randomPastDate(String pattern) {
return randomPastDate(LocalDate.now(), pattern);
}

/**
* 随机以往日期(1年内)
*
* @return 随机以往日期
*/
public Date randomPastDate() {
return randomPastDate(LocalDate.now());
}

/**
* 随机时间
*
Expand All @@ -155,6 +268,18 @@ public LocalDateTime randomTime(int year, int month, int dayOfMonth) {
return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, millisecond);
}

/**
* 随机时间
*
* @param year 年
* @param month 月
* @param dayOfMonth 日
* @return 随机时间
*/
public Date randomDate(int year, int month, int dayOfMonth) {
return localDateTimeToDate(randomTime(year, month, dayOfMonth));
}

/**
* 过去的随机时间(以当天为基准)
*
Expand All @@ -166,6 +291,16 @@ public LocalDateTime randomPastTime(int maxDays) {
return randomPastTime(LocalDateTime.now(), maxDays * 86400);
}

/**
* 过去的随机时间(以当天为基准)
*
* @param maxDays 最大日期间隔
* @return 过去的随机时间
*/
public Date randomPastDate(int maxDays) {
return localDateTimeToDate(randomPastTime(maxDays));
}

/**
* 过去的随机时间
*
Expand All @@ -180,6 +315,17 @@ public LocalDateTime randomPastTime(LocalDateTime base, long maxSeconds) {
return base.minus(second, ChronoUnit.SECONDS).minus(millisecond, ChronoUnit.MILLIS);
}

/**
* 过去的随机时间
*
* @param base 基准时间
* @param maxSeconds 最大相差秒,0或负值表示不限
* @return 过去的随机时间
*/
public Date randomPastDate(LocalDateTime base, long maxSeconds) {
return localDateTimeToDate(randomPastTime(base, maxSeconds));
}

/**
* 未来的随机时间(以当天为基准)
*
Expand All @@ -191,6 +337,16 @@ public LocalDateTime randomFutureTime(int maxDays) {
return randomFutureTime(LocalDateTime.now(), maxDays * 86400);
}

/**
* 未来的随机时间(以当天为基准)
*
* @param maxDays 最大日期间隔
* @return 未来的随机时间
*/
public Date randomFutureDate(int maxDays) {
return localDateTimeToDate(randomFutureTime(maxDays));
}

/**
* 未来的随机时间
*
Expand All @@ -205,6 +361,17 @@ public LocalDateTime randomFutureTime(LocalDateTime base, long maxSeconds) {
return base.plus(second, ChronoUnit.SECONDS).plus(millisecond, ChronoUnit.MILLIS);
}

/**
* 未来的随机时间
*
* @param base 基准时间
* @param maxSeconds 最大相差秒,0或负值表示不限
* @return 未来的随机时间
*/
public Date randomFutureDate(LocalDateTime base, long maxSeconds) {
return localDateTimeToDate(randomFutureTime(base, maxSeconds));
}

/**
* 随机时间戳(毫秒)
*
Expand Down Expand Up @@ -279,4 +446,26 @@ private long randomTimestamp(LocalDateTime base, long maxSeconds) {
long diff = maxSeconds > 0 ? RandomUtils.nextLong(1, maxSeconds * 1000 + 1) : -1 * (RandomUtils.nextLong(1, Math.abs(maxSeconds) * 1000 + 1));
return base.toInstant(ZONE_OFFSET).toEpochMilli() + diff;
}

/**
* 转换LocalDate到Date
*
* @param localDate LocalDate对象
* @return Date对象
*/
private static Date localDateToDate(LocalDate localDate) {
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.of("Asia/Shanghai"));
return Date.from(zonedDateTime.toInstant());
}

/**
* 转换LocalDateTime到Date
*
* @param localDateTime LocalDateTime对象
* @return Date对象
*/
private static Date localDateTimeToDate(LocalDateTime localDateTime) {
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
return Date.from(zonedDateTime.toInstant());
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/apifan/common/random/source/EducationSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public class EducationSource {
private List<String> collegeList;
private static final List<String> numberList = Lists.newArrayList("一", "二", "三", "四", "五", "六", "七", "八", "九", "十");

private List<String> majorList;

private EducationSource() {
collegeList = ResourceUtils.readLines("college.txt");
majorList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("college-major.txt"));
}

/**
Expand Down Expand Up @@ -110,6 +113,15 @@ public String randomClassName() {
return RandomUtils.nextInt(1, 11) + "班";
}

/**
* 获取随机大学专业名称
*
* @return 大学专业名称
*/
public String randomMajorName() {
return ResourceUtils.getRandomElement(majorList);
}

private String getGradeName(int grade) {
Preconditions.checkArgument(grade > 0 && grade < numberList.size(), "grade数字错误");
return numberList.get(grade - 1);
Expand Down
Loading

0 comments on commit 64a347c

Please sign in to comment.