I\'ve a DateTime
that represent the beginning of a recurring event. A Days
(daily period) will represent the recurring period. I assume that this rec
You can always use the add
method in the Calendar
class.
You could do the following -
Date date = "2013-06-27" ;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 3); // add 3 days
date = cal.getTime();
And so on...
This runs all five checks in 180 ms seconds on my machine. Or about 27-checks/second, where you are checking a date 300 years in the future.
@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);
}