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)
| 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. |