MCPcopy Create free account
hub / github.com/DiUS/java-faker / between

Method between

src/main/java/com/github/javafaker/DateAndTime.java:142–153  ·  view source on GitHub ↗

Generates a random date between two dates. @param from the lower bound inclusive @param to the upper bound exclusive @return a random date between from and to. @throws IllegalArgumentException if the to date represents an earlier date than {

(Date from, Date to)

Source from the content-addressed store, hash-verified

140 * if the {@code to} date represents an earlier date than {@code from} date.
141 */
142 public Date between(Date from, Date to) throws IllegalArgumentException {
143 if (to.before(from)) {
144 throw new IllegalArgumentException("Invalid date range, the upper bound date is before the lower bound.");
145 }
146
147 if (from.equals(to)) {
148 return from;
149 }
150
151 long offsetMillis = faker.random().nextLong(to.getTime() - from.getTime());
152 return new Date(from.getTime() + offsetMillis);
153 }
154
155 /**
156 * Generates a random birthday between 65 and 18 years ago.

Callers 2

birthdayMethod · 0.95
testBetweenMethod · 0.80

Calls 3

nextLongMethod · 0.80
beforeMethod · 0.45
randomMethod · 0.45

Tested by 1

testBetweenMethod · 0.64