private static IRecurrence getRecurrenceForDay( int dayOfWeek, StartSpec startSpec, TimeRange timeRange, ICalTimeZone tz, TimeZoneMap tzmap) throws ServiceException { // DTSTART String dateStr = startSpec.getDateString(dayOfWeek); String dtStartStr; if (tz.sameAsUTC()) dtStartStr = String.format("%sT%02d%02d00Z", dateStr, timeRange.start.hour, timeRange.start.minute); else dtStartStr = String.format( "TZID=\"%s\":%sT%02d%02d00", tz.getID(), dateStr, timeRange.start.hour, timeRange.start.minute); ParsedDateTime dtStart; try { dtStart = ParsedDateTime.parse(dtStartStr, tzmap); } catch (ParseException e) { throw ServiceException.INVALID_REQUEST("Bad date/time value \"" + dtStartStr + "\"", e); } // DURATION ParsedDuration dur = timeRange.getDuration(); // RRULE String dayName = DayOfWeekName.lookup(dayOfWeek); String ruleStr = String.format("FREQ=WEEKLY;INTERVAL=1;BYDAY=%s", dayName); return new Recurrence.SimpleRepeatingRule(dtStart, dur, new ZRecur(ruleStr, tzmap), null); }
/** * @param testY candidate year * @param testM candidate month in range 1 to 12 * @param testD candidate day in range 1 to 31 * @param leniant succeed even if the day and month might be ambiguous * @return Formatted date if certain date fields are OK and wouldn't be OK in a different order, * else null */ private String populateDateFieldsIfUnambiguous(int testY, int testM, int testD, boolean leniant) { if ((testY < 1600) || (testY > 4500)) { // Year does not fit within what Outlook allows, probably not what is intended return null; } int firstAllowableDay = leniant ? 1 : 13; if ((testD < firstAllowableDay) || (testD > 31)) { // Either an invalid date or could be a month number if guessed date string order wrong return null; } if ((testM < 1) || (testM > 12)) { // Not a valid month return null; } ICalTimeZone tzUTC = ICalTimeZone.getUTC(); GregorianCalendar cal = new GregorianCalendar(tzUTC); cal.set(testY, testM - 1, 1, 0, 0); if (testD > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) return null; cal.set(Calendar.DAY_OF_MONTH, testD); SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd"); ymd.setCalendar(cal); return ymd.format(cal.getTime()); }