我只使用
java.time.LocalDate
public class RandomDate {
private final LocalDate today;
private final Random random;
private final int months;
public RandomDate(Random random, int months) {
this.today = LocalDate.now();
this.random = random;
this.months = months;
}
public LocalDate getRangeEndDate() {
LocalDate rangeEndDay = this.today.plusDays(this.months * 30);
return rangeEndDay;
}
public LocalDate generateRandomDate(LocalDate endDate) {
int start = (int) this.today.toEpochDay();
int end = (int) endDate.toEpochDay();
long randomDay = start + random.nextInt(end - start);
return LocalDate.ofEpochDay(randomDay);
}
public LocalDate getToday() {
return today;
}
public Random getRandom() {
return random;
}
public int getMonths() {
return months;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RandomDate)) return false;
RandomDate that = (RandomDate) o;
return getMonths() == that.getMonths() &&
Objects.equals(getToday(), that.getToday()) &&
Objects.equals(getRandom(), that.getRandom());
}
@Override
public int hashCode() {
return Objects.hash(getToday(), getRandom(), getMonths());
}
@Override
public String toString() {
return "RandomDate{" +
"today=" + today +
", random=" + random +
", months=" + months +
'}';
}}