use of net.fortuna.ical4j.model.DateTime in project openmeetings by apache.
the class IcalUtils method parseAppointmentstoCalendar.
/**
* Parses a List of Appointments into a VCALENDAR component.
*
* @param appointments List of Appointments for the Calendar
* @param ownerId Owner of the Appointments
* @return VCALENDAR representation of the Appointments
*/
public Calendar parseAppointmentstoCalendar(List<Appointment> appointments, Long ownerId) {
String tzid = parseTimeZone(null, userDao.get(ownerId)).getID();
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
net.fortuna.ical4j.model.TimeZone timeZone = registry.getTimeZone(tzid);
if (timeZone == null) {
throw new NoSuchElementException("Unable to get time zone by id provided: " + tzid);
}
Calendar icsCalendar = new Calendar();
icsCalendar.getProperties().add(new ProdId(PROD_ID));
icsCalendar.getProperties().add(Version.VERSION_2_0);
icsCalendar.getProperties().add(CalScale.GREGORIAN);
icsCalendar.getComponents().add(timeZone.getVTimeZone());
for (Appointment appointment : appointments) {
DateTime start = new DateTime(appointment.getStart()), end = new DateTime(appointment.getEnd());
VEvent meeting = new VEvent(start, end, appointment.getTitle());
meeting = addVEventpropsfromAppointment(appointment, meeting);
icsCalendar.getComponents().add(meeting);
}
return icsCalendar;
}
use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.
the class CalendarImportTest method testImportRecurringCal.
@Test
public void testImportRecurringCal() throws IOException, ParserException {
InputStream in = CalendarImportTest.class.getResourceAsStream("RecurringEvent.ics");
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(in);
assertNotNull(calendar);
VEvent rootEvent = null;
VEvent exceptionEvent = null;
for (Iterator<?> iter = calendar.getComponents().iterator(); iter.hasNext(); ) {
Object comp = iter.next();
if (comp instanceof VEvent) {
VEvent vevent = (VEvent) comp;
if (vevent.getRecurrenceId() == null) {
rootEvent = vevent;
} else {
exceptionEvent = vevent;
}
}
}
assertNotNull(rootEvent);
assertNotNull(exceptionEvent);
java.util.Date startDate = CalendarUtils.getDate(2016, java.util.Calendar.OCTOBER, 10);
DateTime start = new DateTime(startDate);
java.util.Date endDate = CalendarUtils.getDate(2016, java.util.Calendar.NOVEMBER, 10);
DateTime end = new DateTime(endDate);
Period period = new Period(start, end);
PeriodList pList = rootEvent.calculateRecurrenceSet(period);
for (Object obj : pList) {
Period p = (Period) obj;
System.out.println("Period: " + p.getStart());
}
RecurrenceId recurrenceId = exceptionEvent.getRecurrenceId();
Date recurrenceDate = recurrenceId.getDate();
System.out.println("Recurrence: " + recurrenceDate);
exceptionEvent.getSequence();
}
use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.
the class ICalFileCalendarManager method getRecurringEventsInPeriod.
private final List<KalendarRecurEvent> getRecurringEventsInPeriod(KalendarEvent kEvent, Date periodStart, Date periodEnd, TimeZone userTz) {
VEvent vEvent = getVEvent(kEvent);
if (vEvent.getEndDate() == null || vEvent.getStartDate().getDate().after(vEvent.getEndDate().getDate())) {
return Collections.emptyList();
}
// calculate the events in the specified period
Period recurringPeriod = new Period(new DateTime(periodStart), new DateTime(periodEnd));
PeriodList periodList = vEvent.calculateRecurrenceSet(recurringPeriod);
List<KalendarRecurEvent> recurringEvents = new ArrayList<>(periodList.size());
for (Object obj : periodList) {
Period period = (Period) obj;
Date date = period.getStart();
java.util.Calendar eventStartCal = java.util.Calendar.getInstance();
eventStartCal.clear();
eventStartCal.setTime(kEvent.getBegin());
java.util.Calendar eventEndCal = java.util.Calendar.getInstance();
eventEndCal.clear();
eventEndCal.setTime(kEvent.getEnd());
java.util.Calendar recurStartCal = java.util.Calendar.getInstance();
recurStartCal.clear();
if (userTz == null) {
recurStartCal.setTimeInMillis(date.getTime());
} else {
recurStartCal.setTimeInMillis(date.getTime() - userTz.getOffset(date.getTime()));
}
long duration = kEvent.getEnd().getTime() - kEvent.getBegin().getTime();
java.util.Calendar beginCal = java.util.Calendar.getInstance();
beginCal.clear();
beginCal.set(recurStartCal.get(java.util.Calendar.YEAR), recurStartCal.get(java.util.Calendar.MONTH), recurStartCal.get(java.util.Calendar.DATE), eventStartCal.get(java.util.Calendar.HOUR_OF_DAY), eventStartCal.get(java.util.Calendar.MINUTE), eventStartCal.get(java.util.Calendar.SECOND));
java.util.Calendar endCal = java.util.Calendar.getInstance();
endCal.clear();
endCal.setTimeInMillis(beginCal.getTimeInMillis() + duration);
boolean original = false;
if (kEvent.getBegin().compareTo(beginCal.getTime()) == 0) {
// prevent doubled events
original = true;
}
Date recurrenceEnd = getRecurrenceEndDate(kEvent.getRecurrenceRule());
if (kEvent.isAllDayEvent() && recurrenceEnd != null && recurStartCal.getTime().after(recurrenceEnd)) {
// workaround for ical4j-bug in all day events
continue;
}
KalendarRecurEvent recurEvent = new KalendarRecurEvent(kEvent.getID(), original, kEvent.getSubject(), beginCal.getTime(), endCal.getTime());
recurEvent.setOccurenceDate(beginCal.getTime());
recurEvent.setSourceEvent(kEvent);
recurringEvents.add(recurEvent);
}
return recurringEvents;
}
use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.
the class ICalFileCalendarManager method getRecurrenceEndDate.
/**
* @param rule
* @return date of recurrence end
*/
@Override
public Date getRecurrenceEndDate(String rule) {
if (rule != null) {
try {
TimeZone ltz = calendarModule.getDefaultTimeZone();
Recur recur = new Recur(rule);
Date dUntil = recur.getUntil();
DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
if (dtUntil != null) {
if (ltz != null) {
dtUntil.setTimeZone(ltz);
}
return dtUntil;
}
} catch (ParseException e) {
log.error("cannot restore recurrence rule", e);
}
}
return null;
}
use of net.fortuna.ical4j.model.DateTime in project android by nextcloud.
the class SaveCalendar method getDateTime.
private Date getDateTime(Cursor cur, String dbName, String dbTzName, Calendar cal) {
int i = getColumnIndex(cur, dbName);
if (i == -1 || cur.isNull(i)) {
Log_OC.e(TAG, "No valid " + dbName + " column found, index: " + Integer.toString(i));
return null;
}
if (cal == null) {
// Ignore timezone for date-only dates
return utcDateFromMs(cur.getLong(i));
} else if (dbTzName == null) {
Log_OC.e(TAG, "No valid tz " + dbName + " column given");
}
String tz = getString(cur, dbTzName);
final boolean isUtc = isUtcTimeZone(tz);
DateTime dt = new DateTime(isUtc);
if (dt.isUtc() != isUtc) {
throw new RuntimeException("UTC mismatch after construction");
}
dt.setTime(cur.getLong(i));
if (dt.isUtc() != isUtc) {
throw new RuntimeException("UTC mismatch after setTime");
}
if (!isUtc) {
if (mTzRegistry == null) {
mTzRegistry = TimeZoneRegistryFactory.getInstance().createRegistry();
if (mTzRegistry == null) {
throw new RuntimeException("Failed to create TZ registry");
}
}
TimeZone t = mTzRegistry.getTimeZone(tz);
if (t == null) {
Log_OC.e(TAG, "Unknown TZ " + tz + ", assuming UTC");
} else {
dt.setTimeZone(t);
if (!mInsertedTimeZones.contains(t)) {
cal.getComponents().add(t.getVTimeZone());
mInsertedTimeZones.add(t);
}
}
}
return dt;
}
Aggregations