Created
February 17, 2013 17:33
-
-
Save MarkyC/4972419 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Returns a random Date in the range [start, end) | |
| */ | |
| public static Date randomDate(Date start, Date end, boolean endInclusive) { | |
| long d1 = start.getTime(); | |
| long d2 = end.getTime(); | |
| // How can I change the range from [start, end) to [start, end] (inclusive) | |
| // depending on whether endInclusive is set? | |
| //long addYear = endInclusive ? 1 : 0; | |
| long newDate = d1 + (long)(Math.random() * ((d2 - d1) /* + addYear*/)); | |
| return new Date(newDate); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment