Added the sample codes
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class LegacyRandomDateTimes {
|
||||||
|
|
||||||
|
public static Date between(Date startInclusive, Date endExclusive) {
|
||||||
|
long startMillis = startInclusive.getTime();
|
||||||
|
long endMillis = endExclusive.getTime();
|
||||||
|
long randomMillisSinceEpoch = ThreadLocalRandom.current().nextLong(startMillis, endMillis);
|
||||||
|
|
||||||
|
return new Date(randomMillisSinceEpoch);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date timestamp() {
|
||||||
|
return new Date(ThreadLocalRandom.current().nextInt() * 1000L);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class RandomDateTimes {
|
||||||
|
|
||||||
|
public static Instant timestamp() {
|
||||||
|
return Instant.ofEpochSecond(ThreadLocalRandom.current().nextInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Instant between(Instant startInclusive, Instant endExclusive) {
|
||||||
|
long startSeconds = startInclusive.getEpochSecond();
|
||||||
|
long endSeconds = endExclusive.getEpochSecond();
|
||||||
|
long random = ThreadLocalRandom.current().nextLong(startSeconds, endSeconds);
|
||||||
|
|
||||||
|
return Instant.ofEpochSecond(random);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Instant after(Instant startInclusive) {
|
||||||
|
return between(startInclusive, Instant.MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Instant before(Instant upperExclusive) {
|
||||||
|
return between(Instant.MIN, upperExclusive);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class RandomDates {
|
||||||
|
|
||||||
|
public static LocalDate between(LocalDate startInclusive, LocalDate endExclusive) {
|
||||||
|
long startEpochDay = startInclusive.toEpochDay();
|
||||||
|
long endEpochDay = endExclusive.toEpochDay();
|
||||||
|
long randomDay = ThreadLocalRandom.current().nextLong(startEpochDay, endEpochDay);
|
||||||
|
|
||||||
|
return LocalDate.ofEpochDay(randomDay);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalDate date() {
|
||||||
|
int hundredYears = 100 * 365;
|
||||||
|
return LocalDate.ofEpochDay(ThreadLocalRandom.current().nextInt(-hundredYears, hundredYears));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class RandomTimes {
|
||||||
|
|
||||||
|
public static LocalTime between(LocalTime startTime, LocalTime endTime) {
|
||||||
|
int startSeconds = startTime.toSecondOfDay();
|
||||||
|
int endSeconds = endTime.toSecondOfDay();
|
||||||
|
int randomTime = ThreadLocalRandom.current().nextInt(startSeconds, endSeconds);
|
||||||
|
|
||||||
|
return LocalTime.ofSecondOfDay(randomTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalTime time() {
|
||||||
|
return between(LocalTime.MIN, LocalTime.MAX);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.RepeatedTest;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class LegacyRandomDateTimesUnitTest {
|
||||||
|
|
||||||
|
private static final Date MIN_DATE = new Date(Long.MIN_VALUE);
|
||||||
|
private static final Date MAX_DATE = new Date(Long.MAX_VALUE);
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenARange_WhenGenTimestamp_ShouldBeInRange() {
|
||||||
|
long aDay = TimeUnit.DAYS.toMillis(1);
|
||||||
|
long now = new Date().getTime();
|
||||||
|
|
||||||
|
Date hundredYearsAgo = new Date(now - aDay * 365 * 100);
|
||||||
|
Date tenDaysAgo = new Date(now - aDay * 10);
|
||||||
|
|
||||||
|
Date random = LegacyRandomDateTimes.between(hundredYearsAgo, tenDaysAgo);
|
||||||
|
assertThat(random).isBetween(hundredYearsAgo, tenDaysAgo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenNoRange_WhenGenTimestamp_ShouldGenerateRandomTimestamps() {
|
||||||
|
Date random = LegacyRandomDateTimes.timestamp();
|
||||||
|
|
||||||
|
assertThat(random)
|
||||||
|
.isNotNull()
|
||||||
|
.isBetween(MIN_DATE, MAX_DATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.RepeatedTest;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class RandomDateTimesUnitTest {
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenNoRange_WhenGenTimestamp_ShouldGenerateRandomTimestamps() {
|
||||||
|
Instant random = RandomDateTimes.timestamp();
|
||||||
|
|
||||||
|
assertThat(random).isBetween(Instant.MIN, Instant.MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenARange_WhenGenTimestamp_ShouldBeInRange() {
|
||||||
|
Instant hundredYearsAgo = Instant.now().minus(Duration.ofDays(100 * 365));
|
||||||
|
Instant tenDaysAgo = Instant.now().minus(Duration.ofDays(10));
|
||||||
|
|
||||||
|
Instant random = RandomDateTimes.between(hundredYearsAgo, tenDaysAgo);
|
||||||
|
assertThat(random).isBetween(hundredYearsAgo, tenDaysAgo);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.RepeatedTest;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class RandomDatesUnitTest {
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenNoRange_WhenGenDate_ShouldGenerateRandomDates() {
|
||||||
|
LocalDate randomDay = RandomDates.date();
|
||||||
|
|
||||||
|
assertThat(randomDay).isAfter(LocalDate.MIN).isBefore(LocalDate.MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenARange_WhenGenDate_ShouldBeInRange() {
|
||||||
|
LocalDate start = LocalDate.of(1989, Month.OCTOBER, 14);
|
||||||
|
LocalDate end = LocalDate.now();
|
||||||
|
|
||||||
|
LocalDate random = RandomDates.between(start, end);
|
||||||
|
assertThat(random).isAfter(start).isBefore(end);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.random;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.RepeatedTest;
|
||||||
|
|
||||||
|
import java.time.LocalTime;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class RandomTimesUnitTest {
|
||||||
|
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void givenARange_WhenGenTime_ShouldBeInRange() {
|
||||||
|
LocalTime morning = LocalTime.of(8, 30);
|
||||||
|
LocalTime randomTime = RandomTimes.between(LocalTime.MIDNIGHT, morning);
|
||||||
|
|
||||||
|
assertThat(randomTime)
|
||||||
|
.isAfter(LocalTime.MIDNIGHT).isBefore(morning)
|
||||||
|
.isAfter(LocalTime.MIN).isBefore(LocalTime.MAX);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user