File tree Expand file tree Collapse file tree 8 files changed +195
-0
lines changed
core-java-modules/core-java-datetime-java8/src
main/java/com/baeldung/random
test/java/com/baeldung/random Expand file tree Collapse file tree 8 files changed +195
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import java .util .Date ;
4
+ import java .util .concurrent .ThreadLocalRandom ;
5
+
6
+ public class LegacyRandomDateTimes {
7
+
8
+ public static Date between (Date startInclusive , Date endExclusive ) {
9
+ long startMillis = startInclusive .getTime ();
10
+ long endMillis = endExclusive .getTime ();
11
+ long randomMillisSinceEpoch = ThreadLocalRandom .current ().nextLong (startMillis , endMillis );
12
+
13
+ return new Date (randomMillisSinceEpoch );
14
+ }
15
+
16
+ public static Date timestamp () {
17
+ return new Date (ThreadLocalRandom .current ().nextInt () * 1000L );
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import java .time .Instant ;
4
+ import java .util .concurrent .ThreadLocalRandom ;
5
+
6
+ public class RandomDateTimes {
7
+
8
+ public static Instant timestamp () {
9
+ return Instant .ofEpochSecond (ThreadLocalRandom .current ().nextInt ());
10
+ }
11
+
12
+ public static Instant between (Instant startInclusive , Instant endExclusive ) {
13
+ long startSeconds = startInclusive .getEpochSecond ();
14
+ long endSeconds = endExclusive .getEpochSecond ();
15
+ long random = ThreadLocalRandom .current ().nextLong (startSeconds , endSeconds );
16
+
17
+ return Instant .ofEpochSecond (random );
18
+ }
19
+
20
+ public static Instant after (Instant startInclusive ) {
21
+ return between (startInclusive , Instant .MAX );
22
+ }
23
+
24
+ public static Instant before (Instant upperExclusive ) {
25
+ return between (Instant .MIN , upperExclusive );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import java .time .LocalDate ;
4
+ import java .util .concurrent .ThreadLocalRandom ;
5
+
6
+ public class RandomDates {
7
+
8
+ public static LocalDate between (LocalDate startInclusive , LocalDate endExclusive ) {
9
+ long startEpochDay = startInclusive .toEpochDay ();
10
+ long endEpochDay = endExclusive .toEpochDay ();
11
+ long randomDay = ThreadLocalRandom .current ().nextLong (startEpochDay , endEpochDay );
12
+
13
+ return LocalDate .ofEpochDay (randomDay );
14
+ }
15
+
16
+ public static LocalDate date () {
17
+ int hundredYears = 100 * 365 ;
18
+ return LocalDate .ofEpochDay (ThreadLocalRandom .current ().nextInt (-hundredYears , hundredYears ));
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import java .time .LocalTime ;
4
+ import java .util .concurrent .ThreadLocalRandom ;
5
+
6
+ public class RandomTimes {
7
+
8
+ public static LocalTime between (LocalTime startTime , LocalTime endTime ) {
9
+ int startSeconds = startTime .toSecondOfDay ();
10
+ int endSeconds = endTime .toSecondOfDay ();
11
+ int randomTime = ThreadLocalRandom .current ().nextInt (startSeconds , endSeconds );
12
+
13
+ return LocalTime .ofSecondOfDay (randomTime );
14
+ }
15
+
16
+ public static LocalTime time () {
17
+ return between (LocalTime .MIN , LocalTime .MAX );
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import org .junit .jupiter .api .RepeatedTest ;
4
+
5
+ import java .util .Date ;
6
+ import java .util .concurrent .TimeUnit ;
7
+
8
+ import static org .assertj .core .api .Assertions .assertThat ;
9
+
10
+ class LegacyRandomDateTimesUnitTest {
11
+
12
+ private static final Date MIN_DATE = new Date (Long .MIN_VALUE );
13
+ private static final Date MAX_DATE = new Date (Long .MAX_VALUE );
14
+
15
+ @ RepeatedTest (100 )
16
+ void givenARange_WhenGenTimestamp_ShouldBeInRange () {
17
+ long aDay = TimeUnit .DAYS .toMillis (1 );
18
+ long now = new Date ().getTime ();
19
+
20
+ Date hundredYearsAgo = new Date (now - aDay * 365 * 100 );
21
+ Date tenDaysAgo = new Date (now - aDay * 10 );
22
+
23
+ Date random = LegacyRandomDateTimes .between (hundredYearsAgo , tenDaysAgo );
24
+ assertThat (random ).isBetween (hundredYearsAgo , tenDaysAgo );
25
+ }
26
+
27
+ @ RepeatedTest (100 )
28
+ void givenNoRange_WhenGenTimestamp_ShouldGenerateRandomTimestamps () {
29
+ Date random = LegacyRandomDateTimes .timestamp ();
30
+
31
+ assertThat (random )
32
+ .isNotNull ()
33
+ .isBetween (MIN_DATE , MAX_DATE );
34
+ }
35
+
36
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import org .junit .jupiter .api .RepeatedTest ;
4
+
5
+ import java .time .Duration ;
6
+ import java .time .Instant ;
7
+
8
+ import static org .assertj .core .api .Assertions .assertThat ;
9
+
10
+ class RandomDateTimesUnitTest {
11
+
12
+ @ RepeatedTest (100 )
13
+ void givenNoRange_WhenGenTimestamp_ShouldGenerateRandomTimestamps () {
14
+ Instant random = RandomDateTimes .timestamp ();
15
+
16
+ assertThat (random ).isBetween (Instant .MIN , Instant .MAX );
17
+ }
18
+
19
+ @ RepeatedTest (100 )
20
+ void givenARange_WhenGenTimestamp_ShouldBeInRange () {
21
+ Instant hundredYearsAgo = Instant .now ().minus (Duration .ofDays (100 * 365 ));
22
+ Instant tenDaysAgo = Instant .now ().minus (Duration .ofDays (10 ));
23
+
24
+ Instant random = RandomDateTimes .between (hundredYearsAgo , tenDaysAgo );
25
+ assertThat (random ).isBetween (hundredYearsAgo , tenDaysAgo );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import org .junit .jupiter .api .RepeatedTest ;
4
+
5
+ import java .time .LocalDate ;
6
+ import java .time .Month ;
7
+
8
+ import static org .assertj .core .api .Assertions .assertThat ;
9
+
10
+ class RandomDatesUnitTest {
11
+
12
+ @ RepeatedTest (100 )
13
+ void givenNoRange_WhenGenDate_ShouldGenerateRandomDates () {
14
+ LocalDate randomDay = RandomDates .date ();
15
+
16
+ assertThat (randomDay ).isAfter (LocalDate .MIN ).isBefore (LocalDate .MAX );
17
+ }
18
+
19
+ @ RepeatedTest (100 )
20
+ void givenARange_WhenGenDate_ShouldBeInRange () {
21
+ LocalDate start = LocalDate .of (1989 , Month .OCTOBER , 14 );
22
+ LocalDate end = LocalDate .now ();
23
+
24
+ LocalDate random = RandomDates .between (start , end );
25
+ assertThat (random ).isAfter (start ).isBefore (end );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .random ;
2
+
3
+ import org .junit .jupiter .api .RepeatedTest ;
4
+
5
+ import java .time .LocalTime ;
6
+
7
+ import static org .assertj .core .api .Assertions .assertThat ;
8
+
9
+ class RandomTimesUnitTest {
10
+
11
+ @ RepeatedTest (100 )
12
+ void givenARange_WhenGenTime_ShouldBeInRange () {
13
+ LocalTime morning = LocalTime .of (8 , 30 );
14
+ LocalTime randomTime = RandomTimes .between (LocalTime .MIDNIGHT , morning );
15
+
16
+ assertThat (randomTime )
17
+ .isAfter (LocalTime .MIDNIGHT ).isBefore (morning )
18
+ .isAfter (LocalTime .MIN ).isBefore (LocalTime .MAX );
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments