private static void copyModelToEventRecurrence(final RecurrenceModel model, EventRecurrence er) {
    if (model.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
      throw new IllegalStateException("There's no recurrence");
    }

    // Freq
    er.freq = mFreqModelToEventRecurrence[model.freq];

    // Interval
    if (model.interval <= 1) {
      er.interval = 0;
    } else {
      er.interval = model.interval;
    }

    // End
    switch (model.end) {
      case RecurrenceModel.END_BY_DATE:
        if (model.endDate != null) {
          model.endDate.switchTimezone(Time.TIMEZONE_UTC);
          model.endDate.normalize(false);
          er.until = model.endDate.format2445();
          er.count = 0;
        } else {
          throw new IllegalStateException("end = END_BY_DATE but endDate is null");
        }
        break;
      case RecurrenceModel.END_BY_COUNT:
        er.count = model.endCount;
        er.until = null;
        if (er.count <= 0) {
          throw new IllegalStateException("count is " + er.count);
        }
        break;
      default:
        er.count = 0;
        er.until = null;
        break;
    }

    // Weekly && monthly repeat patterns
    er.bydayCount = 0;
    er.bymonthdayCount = 0;

    switch (model.freq) {
      case RecurrenceModel.FREQ_MONTHLY:
        if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_DATE) {
          if (model.monthlyByMonthDay > 0) {
            if (er.bymonthday == null || er.bymonthdayCount < 1) {
              er.bymonthday = new int[1];
            }
            er.bymonthday[0] = model.monthlyByMonthDay;
            er.bymonthdayCount = 1;
          }
        } else if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
          if (!isSupportedMonthlyByNthDayOfWeek(model.monthlyByNthDayOfWeek)) {
            throw new IllegalStateException(
                "month repeat by nth week but n is " + model.monthlyByNthDayOfWeek);
          }
          int count = 1;
          if (er.bydayCount < count || er.byday == null || er.bydayNum == null) {
            er.byday = new int[count];
            er.bydayNum = new int[count];
          }
          er.bydayCount = count;
          er.byday[0] = EventRecurrence.timeDay2Day(model.monthlyByDayOfWeek);
          er.bydayNum[0] = model.monthlyByNthDayOfWeek;
        }
        break;
      case RecurrenceModel.FREQ_WEEKLY:
        int count = 0;
        for (int i = 0; i < 7; i++) {
          if (model.weeklyByDayOfWeek[i]) {
            count++;
          }
        }

        if (er.bydayCount < count || er.byday == null || er.bydayNum == null) {
          er.byday = new int[count];
          er.bydayNum = new int[count];
        }
        er.bydayCount = count;

        for (int i = 6; i >= 0; i--) {
          if (model.weeklyByDayOfWeek[i]) {
            er.bydayNum[--count] = 0;
            er.byday[count] = EventRecurrence.timeDay2Day(i);
          }
        }
        break;
    }

    if (!canHandleRecurrenceRule(er)) {
      throw new IllegalStateException(
          "UI generated recurrence that it can't handle. ER:"
              + er.toString()
              + " Model: "
              + model.toString());
    }
  }