public static Calendar createCalendar(CalDavEvent calDavEvent, DateTimeZone timeZone) { TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); TimeZone timezone = registry.getTimeZone(timeZone.getID()); Calendar calendar = new Calendar(); calendar.getProperties().add(Version.VERSION_2_0); calendar.getProperties().add(new ProdId("openHAB")); VEvent vEvent = new VEvent(); vEvent.getProperties().add(new Summary(calDavEvent.getName())); vEvent.getProperties().add(new Description(calDavEvent.getContent())); final DtStart dtStart = new DtStart(new net.fortuna.ical4j.model.DateTime(calDavEvent.getStart().toDate())); dtStart.setTimeZone(timezone); vEvent.getProperties().add(dtStart); final DtEnd dtEnd = new DtEnd(new net.fortuna.ical4j.model.DateTime(calDavEvent.getEnd().toDate())); dtEnd.setTimeZone(timezone); vEvent.getProperties().add(dtEnd); vEvent.getProperties().add(new Uid(calDavEvent.getId())); vEvent.getProperties().add(Clazz.PUBLIC); vEvent .getProperties() .add( new LastModified( new net.fortuna.ical4j.model.DateTime(calDavEvent.getLastChanged().toDate()))); calendar.getComponents().add(vEvent); return calendar; }
public final void test() { // create event start date.. java.util.Calendar calendar = getCalendarInstance(); calendar.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER); calendar.set(java.util.Calendar.DAY_OF_MONTH, 25); DtStart start = new DtStart(new Date(calendar.getTime())); start.getParameters().add(tzParam); start.getParameters().add(Value.DATE); Summary summary = new Summary("Christmas Day; \n this is a, test\\"); VEvent christmas = new VEvent(); christmas.getProperties().add(start); christmas.getProperties().add(summary); log.info(christmas); }
/** * Fügt einem iCal-Event das Startdatum aus dem übergebenen Appointment-Objekt hinzu. * * @param appointment */ private void addStartDateToEvent( Appointment appointment, PropertyList properties, boolean isAllDayEvent) { Date startDate = appointment.getStart(); if (isAllDayEvent) { DtStart start = getDtStartFromAllDayEvent(startDate, calendar); properties.add(start); } else { if (appointment.getRepeating() == null) { DtStart start = new DtStart(convertRaplaLocaleToUTC(startDate)); start.setUtc(true); properties.add(start); } else { DtStart start = getStartDateProperty(startDate); properties.add(start); } } }
@Override @SuppressWarnings("unchecked") public ByteArrayOutputStream toEntity() throws IOException { net.fortuna.ical4j.model.Calendar ical = new net.fortuna.ical4j.model.Calendar(); ical.getProperties().add(Version.VERSION_2_0); ical.getProperties() .add(new ProdId("-//bitfire web engineering//DAVdroid " + Constants.APP_VERSION + "//EN")); VEvent event = new VEvent(); PropertyList props = event.getProperties(); if (uid != null) props.add(new Uid(uid)); props.add(dtStart); if (dtEnd != null) props.add(dtEnd); if (duration != null) props.add(duration); if (rrule != null) props.add(rrule); if (rdate != null) props.add(rdate); if (exrule != null) props.add(exrule); if (exdate != null) props.add(exdate); if (summary != null && !summary.isEmpty()) props.add(new Summary(summary)); if (location != null && !location.isEmpty()) props.add(new Location(location)); if (description != null && !description.isEmpty()) props.add(new Description(description)); if (status != null) props.add(status); if (!opaque) props.add(Transp.TRANSPARENT); if (organizer != null) props.add(organizer); props.addAll(attendees); if (forPublic != null) event.getProperties().add(forPublic ? Clazz.PUBLIC : Clazz.PRIVATE); event.getAlarms().addAll(alarms); props.add(new LastModified()); ical.getComponents().add(event); // add VTIMEZONE components net.fortuna.ical4j.model.TimeZone tzStart = (dtStart == null ? null : dtStart.getTimeZone()), tzEnd = (dtEnd == null ? null : dtEnd.getTimeZone()); if (tzStart != null) ical.getComponents().add(tzStart.getVTimeZone()); if (tzEnd != null && tzEnd != tzStart) ical.getComponents().add(tzEnd.getVTimeZone()); CalendarOutputter output = new CalendarOutputter(false); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { output.output(ical, os); } catch (ValidationException e) { Log.e(TAG, "Generated invalid iCalendar"); } return os; }
public long getDtStartInMillis() { return dtStart.getDate().getTime(); }
@Override @SuppressWarnings("unchecked") public void parseEntity(@NonNull InputStream entity) throws IOException, InvalidResourceException { net.fortuna.ical4j.model.Calendar ical; try { CalendarBuilder builder = new CalendarBuilder(); ical = builder.build(entity); if (ical == null) throw new InvalidResourceException("No iCalendar found"); } catch (ParserException e) { throw new InvalidResourceException(e); } // event ComponentList events = ical.getComponents(Component.VEVENT); if (events == null || events.isEmpty()) throw new InvalidResourceException("No VEVENT found"); VEvent event = (VEvent) events.get(0); if (event.getUid() != null) uid = event.getUid().getValue(); else { Log.w(TAG, "Received VEVENT without UID, generating new one"); generateUID(); } if ((dtStart = event.getStartDate()) == null || (dtEnd = event.getEndDate()) == null) throw new InvalidResourceException("Invalid start time/end time/duration"); if (hasTime(dtStart)) { validateTimeZone(dtStart); validateTimeZone(dtEnd); } // all-day events and "events on that day": // * related UNIX times must be in UTC // * must have a duration (set to one day if missing) if (!hasTime(dtStart) && !dtEnd.getDate().after(dtStart.getDate())) { Log.i(TAG, "Repairing iCal: DTEND := DTSTART+1"); Calendar c = Calendar.getInstance(TimeZone.getTimeZone(Time.TIMEZONE_UTC)); c.setTime(dtStart.getDate()); c.add(Calendar.DATE, 1); dtEnd.setDate(new Date(c.getTimeInMillis())); } rrule = (RRule) event.getProperty(Property.RRULE); rdate = (RDate) event.getProperty(Property.RDATE); exrule = (ExRule) event.getProperty(Property.EXRULE); exdate = (ExDate) event.getProperty(Property.EXDATE); if (event.getSummary() != null) summary = event.getSummary().getValue(); if (event.getLocation() != null) location = event.getLocation().getValue(); if (event.getDescription() != null) description = event.getDescription().getValue(); status = event.getStatus(); opaque = true; if (event.getTransparency() == Transp.TRANSPARENT) opaque = false; organizer = event.getOrganizer(); for (Object o : event.getProperties(Property.ATTENDEE)) attendees.add((Attendee) o); Clazz classification = event.getClassification(); if (classification != null) { if (classification == Clazz.PUBLIC) forPublic = true; else if (classification == Clazz.CONFIDENTIAL || classification == Clazz.PRIVATE) forPublic = false; } this.alarms = event.getAlarms(); }