首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在一个时间范围内分配最多两个ids

如何在一个时间范围内分配最多两个ids
EN

Stack Overflow用户
提问于 2014-01-16 16:37:03
回答 1查看 43关注 0票数 0

这是一个我能理解的编程问题。上下文如下-

有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。

我被困在这里了

代码语言:javascript
复制
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);
    }

}

请提个建议。谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-01-16 17:32:36

最简单的解决方案是将时间分解为两个小时周期和几天的一部分。

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21156877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档