我有DateTime
代表重复活动的开始。 Days
(每日期)代表经常性的时期。我认为这种反复发生的事件永远不会停止。
from = "2013-06-27"
period = 3 days
nextOccurence will be "2013-06-30", "2013-07-03", "2013-07-06", and so on.
"2013-07-03" is an occurence but "2013-07-04" isn't an occurence.
我想知道在性能方面最好的方法是确定DateTime
是否是重复发生的事件?从长远来看,该计划将需要检查“2014-07-03”或“2015-07-03”是否出现。
答案 0 :(得分:1)
这会在我的机器上以180毫秒的秒数运行所有五项检查。或者大约27支/秒,你在那里查看将来300年的日期。
@Test
public void isOccurrence() {
long startTime = System.currentTimeMillis();
assertTrue(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2010, 1, 19, 0, 0)));
assertFalse(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2010, 1, 18, 0, 0)));
assertTrue(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2310, 1, 19, 0, 0)));
assertFalse(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2310, 1, 20, 0, 0)));
assertTrue(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2010, 1, 10, 0, 0)));
System.out.println("elapsed=" + (System.currentTimeMillis() - startTime));
}
public boolean isOccurrence(DateMidnight startDate, int dayIncrement, DateTime testTime) {
DateMidnight testDateMidnight = testTime.toDateMidnight();
while (startDate.isBefore(testDateMidnight)) {
startDate = startDate.plusDays(dayIncrement);
}
return startDate.equals(testDateMidnight);
}
答案 1 :(得分:0)
您始终可以使用add
类中的Calendar
方法。
您可以执行以下操作 -
Date date = "2013-06-27" ;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 3); // add 3 days
date = cal.getTime();
等等......