{
Assert.that(minInterval >= 0 && minInterval < Date.MillisPerHour,
"minInterval must be positive and less than one hour");
long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime);
// If current time in recycle window, return minimum interval.
int hour = Date.getHours(date);
if (inRecycleWindow(hour)) {
return minInterval;
}
// Set date to the recycle window begin hour on current day. We add 1 second
// to avoid rounding errors. Compute interval to next begin time. If begin hour
// is before current time, interval will be negative, so add a day to adjust.
Date.setHoursMinutesSeconds(date, recycleWindowBeginHour, 0, 1);
long interval = date.getTime() - currentTime;
interval = (interval >= 0) ? interval : interval + Date.MillisPerDay;
Assert.assertNonFatal(interval >= 0, Fmt.S("Got negative interval %s", interval));
// Add a random value between 0 and 10 minutes to make it less likely that two
// nodes will try at the very beginning of the recycle window.
interval += (new Random().nextInt(600)) * Date.MillisPerSecond;