这是一个我能理解的编程问题。上下文如下-
有TimeWindow,即从11:00到13:00。我有一个I列表,要在未来几天内分发,但只能在指定的时间窗口之间分发。
我使用一个随机数生成器来获取随机数。我使用JodaTime Api来计算日期和时间。
至于ex。从今天开始,我必须在接下来的几天内分发I。生成的第一个随机数是99。所以分配给第一个Id的时间是2014年1月16日,(11:00+99mins)= 12:39分钟。现在假设它将生成110作为随机数。现在( 12:39+ 110) >大于同一天的13:00小时。所以它应该从11:00开始分配第二天的时间。因此,我们已经在接下来的几天内分发了列表中的所有in。
我被困在这里了
int[] startHour= {11, 0};
int[] endtHour= {13, 0};
DateTimeZone dtZoneforUser = DateTimeZone.forID("America/New_York");
DateTime dtNow = DateTime.now(dtZoneforUser);
DateTime dtTimeWindowStart = new DateTime(dtNow.getYear(),dtNow.getMonthOfYear(),dtNow.dayOfMonth().get(),startHour[0],startHour[1],dtZoneforUser);
DateTime dtTimeWindowEnd = new DateTime(dtNow.getYear(),dtNow.getMonthOfYear(),dtNow.dayOfMonth().get(),endtHour[0],endtHour[1],dtZoneforUser);
List<String> lstAudio = new ArrayList<String>();
lstAudio.add("1");
lstAudio.add("2");
lstAudio.add("3");
lstAudio.add("4");
DateTime recurringStartingTime = dtTimeWindowStart;
DateTime recurringEndTime = dtTimeWindowEnd;
List<String> timeToPlay = new ArrayList<String>();
int daysCounter = 0;
for (String audioVal : lstAudio) {
int randomNum = DateTimeFormatTestIntellix.getRandom();
DateTime tempTime = recurringStartingTime.plusMinutes(randomNum);
recurringStartingTime = tempTime;
System.out.println(randomNum);
if ( tempTime.isBefore(recurringEndTime) )
{
System.out.println("Audio Id is "+audioVal + " and the play time is "+tempTime);
}
}
请提个建议。谢谢
发布于 2014-01-16 17:32:36
最简单的解决方案是将时间分解为两个小时周期和几天的一部分。
int period = 2 * 60;
int start = 0;
for (String audioVal : lstAudio) {
int randomNum = DateTimeFormatTestIntellix.getRandom();
start += randomNum;
int days = start / period;
int timeInDay = start % period;
// turn the days and timeInDay into a DateTime
}
https://stackoverflow.com/questions/21156877
复制相似问题