Skip to content

Instantly share code, notes, and snippets.

@MarkyC
Created February 17, 2013 17:33
Show Gist options
  • Select an option

  • Save MarkyC/4972419 to your computer and use it in GitHub Desktop.

Select an option

Save MarkyC/4972419 to your computer and use it in GitHub Desktop.
/**
* 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