Merge branch 'master' into BAEL-3442
This commit is contained in:
@@ -13,3 +13,4 @@ This module contains articles about the Date and Time API introduced with Java 8
|
||||
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
|
||||
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
|
||||
- [Comparing Dates in Java](https://www.baeldung.com/java-comparing-dates)
|
||||
- [Generating Random Dates in Java](https://www.baeldung.com/java-random-dates)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-datetime-java8</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
@@ -61,10 +62,11 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -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