private static long invokeRecurrence(RRule rrule, DateTime original, DateValue startDateAsDV) {
    long newDueDate = -1;
    RecurrenceIterator iterator =
        RecurrenceIteratorFactory.createRecurrenceIterator(
            rrule, startDateAsDV, TimeZone.getDefault());
    DateValue nextDate;

    for (int i = 0; i < 10; i++) { // ten tries then we give up
      if (!iterator.hasNext()) {
        return -1;
      }
      nextDate = iterator.next();

      if (nextDate.compareTo(startDateAsDV) == 0) {
        continue;
      }

      newDueDate = buildNewDueDate(original, nextDate);

      // detect if we finished
      if (newDueDate > original.getMillis()) {
        break;
      }
    }
    return newDueDate;
  }
 /** Compute long due date from DateValue */
 private static long buildNewDueDate(DateTime original, DateValue nextDate) {
   long newDueDate;
   if (nextDate instanceof DateTimeValueImpl) {
     DateTimeValueImpl newDateTime = (DateTimeValueImpl) nextDate;
     DateTime date =
         newDateUtc(
                 newDateTime.year(),
                 newDateTime.month(),
                 newDateTime.day(),
                 newDateTime.hour(),
                 newDateTime.minute(),
                 newDateTime.second())
             .toLocal();
     // time may be inaccurate due to DST, force time to be same
     date =
         date.withHourOfDay(original.getHourOfDay()).withMinuteOfHour(original.getMinuteOfHour());
     newDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, date.getMillis());
   } else {
     newDueDate =
         Task.createDueDate(
             Task.URGENCY_SPECIFIC_DAY,
             newDate(nextDate.year(), nextDate.month(), nextDate.day()).getMillis());
   }
   return newDueDate;
 }
 /** Compute long due date from DateValue */
 private static long buildNewDueDate(Date original, DateValue nextDate) {
   long newDueDate;
   if (nextDate instanceof DateTimeValueImpl) {
     DateTimeValueImpl newDateTime = (DateTimeValueImpl) nextDate;
     Date date =
         new Date(
             Date.UTC(
                 newDateTime.year() - 1900,
                 newDateTime.month() - 1,
                 newDateTime.day(),
                 newDateTime.hour(),
                 newDateTime.minute(),
                 newDateTime.second()));
     // time may be inaccurate due to DST, force time to be same
     date.setHours(original.getHours());
     date.setMinutes(original.getMinutes());
     newDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, date.getTime());
   } else {
     newDueDate =
         Task.createDueDate(
             Task.URGENCY_SPECIFIC_DAY,
             new Date(nextDate.year() - 1900, nextDate.month() - 1, nextDate.day()).getTime());
   }
   return newDueDate;
 }