net.fortuna.ical4j.model.Date Java Examples
The following examples show how to use
net.fortuna.ical4j.model.Date.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecurrenceExpander.java From cosmo with Apache License 2.0 | 6 votes |
/** * Return start and end Date that represent the start of the first * occurrence of a recurring component and the end of the last * occurence. If the recurring component has no end(infinite recurring event), * then no end date will be returned. * @param calendar Calendar containing master and modification components * @return array containing start (located at index 0) and end (index 1) of * recurring component. */ public Date[] calculateRecurrenceRange(Calendar calendar) { try{ ComponentList<VEvent> vevents = calendar.getComponents().getComponents(Component.VEVENT); List<Component> exceptions = new ArrayList<Component>(); Component masterComp = null; // get list of exceptions (VEVENT with RECURRENCEID) for (Iterator<VEvent> i = vevents.iterator(); i.hasNext();) { VEvent event = i.next(); if (event.getRecurrenceId() != null) { exceptions.add(event); } else { masterComp = event; } } return calculateRecurrenceRange(masterComp, exceptions); } catch (Exception e){ LOG.error("ERROR in calendar: " + calendar, e); throw e; } }
Example #2
Source File: EntityConverter.java From cosmo with Apache License 2.0 | 6 votes |
/** * Updates note modification. * @param noteMod The note item modified. * @param event The event. */ private void updateNoteModification(NoteItem noteMod, VEvent event) { EventExceptionStamp exceptionStamp = StampUtils.getEventExceptionStamp(noteMod); exceptionStamp.setExceptionEvent(event); // copy VTIMEZONEs to front if present ComponentList<VTimeZone> vtimezones = exceptionStamp.getMasterStamp() .getEventCalendar().getComponents(Component.VTIMEZONE); for(VTimeZone vtimezone : vtimezones) { exceptionStamp.getEventCalendar().getComponents().add(0, vtimezone); } noteMod.setClientModifiedDate(new Date()); noteMod.setLastModifiedBy(noteMod.getModifies().getLastModifiedBy()); noteMod.setLastModification(ContentItem.Action.EDITED); setCalendarAttributes(noteMod, event); }
Example #3
Source File: MockBaseEventStamp.java From cosmo with Apache License 2.0 | 6 votes |
/** * Sets date property value * @param prop The date property. * @param date The date. */ protected void setDatePropertyValue(DateProperty prop, Date date) { if (prop == null) { return; } Value value = (Value) prop.getParameters() .getParameter(Parameter.VALUE); if (value != null) { prop.getParameters().remove(value); } // Add VALUE=DATE for Date values, otherwise // leave out VALUE=DATE-TIME because it is redundant if (!(date instanceof DateTime)) { prop.getParameters().add(Value.DATE); } }
Example #4
Source File: ModificationUidImpl.java From cosmo with Apache License 2.0 | 6 votes |
/** * Converts an ical4j Date instance to a string representation. If the instance * is a DateTime and has a timezone, then the string representation will be * UTC. * @param date date to format * @return string representation of date */ public static String fromDateToStringNoTimezone(Date date) { if(date==null) { return null; } if(date instanceof DateTime) { DateTime dt = (DateTime) date; // If DateTime has a timezone, then convert to UTC before // serializing as String. if(dt.getTimeZone()!=null) { // clone instance first to prevent changes to original instance DateTime copy = new DateTime(dt); copy.setUtc(true); return copy.toString(); } else { return dt.toString(); } } else { return date.toString(); } }
Example #5
Source File: EventResource.java From XPagesExtensionLibrary with Apache License 2.0 | 6 votes |
/** * @param recurIdList * @param iterator The Data in Iterator should be Date or DateTime */ public void removeRecurIDListbyDate(ArrayList<String> recurIdList, DateList exDates) { String sDateTime; Iterator iterator = exDates.iterator(); while (iterator.hasNext()) { Date exDate = (Date)iterator.next(); if(exDate instanceof DateTime){ ((DateTime) exDate).setUtc(true); } // exDates must have same type with value sDateTime = exDate.toString(); int positionT = sDateTime.indexOf("T"); if(positionT>0){ sDateTime = sDateTime.substring(0, positionT); } recurIdList.remove(sDateTime); } }
Example #6
Source File: ThisAndFutureHelper.java From cosmo with Apache License 2.0 | 6 votes |
/** * Given an existing recurring series and new series, break the * existing series at the given occurrence and move all modifications * from the existing series that apply to the new series to the * new series. * @param oldSeries note representing recurring series to break * @param newSeries note representing new series * @param occurrence occurrence of old series * (NoteOccurrence or NoteItem modification) to break old * series at. * @return Set of modifications that need to be removed and added. * Removals are indicated with isActive==false. * All other active NoteItems are considered additions. */ public Set<NoteItem> breakRecurringEvent(NoteItem oldSeries, NoteItem newSeries, NoteItem occurrence) { Date lastRid = null; if(occurrence instanceof NoteOccurrence) { lastRid = ((NoteOccurrence) occurrence).getOccurrenceDate(); } else { EventExceptionStamp ees = StampUtils.getEventExceptionStamp(occurrence); if(ees==null) { throw new IllegalArgumentException("occurence must have an event stamp"); } lastRid = ees.getRecurrenceId(); } return breakRecurringEvent(oldSeries, newSeries, lastRid); }
Example #7
Source File: ThisAndFutureHelper.java From cosmo with Apache License 2.0 | 6 votes |
private void modifyOldSeries(NoteItem oldSeries, Date lastRecurrenceId) { EventStamp event = StampUtils.getEventStamp(oldSeries); // We set the end date to 1 second before the begining of the next day java.util.Calendar untilDateCalendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT")); untilDateCalendar.setTime(lastRecurrenceId); untilDateCalendar.add(java.util.Calendar.DAY_OF_MONTH, -1); untilDateCalendar.set(java.util.Calendar.HOUR_OF_DAY, 23); untilDateCalendar.set(java.util.Calendar.MINUTE, 59); untilDateCalendar.set(java.util.Calendar.SECOND, 59); Date untilDate = Dates.getInstance(untilDateCalendar.getTime(), lastRecurrenceId); // UNTIL must be UTC according to spec if(untilDate instanceof DateTime) { ((DateTime) untilDate).setUtc(true); } List<Recur> recurs = event.getRecurrenceRules(); for (Recur recur : recurs) { recur.setUntil(untilDate); } // TODO: Figure out what to do with RDATEs }
Example #8
Source File: EntityConverter.java From cosmo with Apache License 2.0 | 6 votes |
/** * Gets modification. * @param masterNote The master note. * @param recurrenceId The reccurence id. * @return The note item. */ private NoteItem getModification(NoteItem masterNote, Date recurrenceId) { for (NoteItem mod : masterNote.getModifications()) { EventExceptionStamp exceptionStamp = StampUtils.getEventExceptionStamp(mod); // only interested in mods with event stamp if (exceptionStamp == null) { continue; } if (exceptionStamp.getRecurrenceId().equals(recurrenceId)) { return mod; } } return null; }
Example #9
Source File: EntityConverter.java From cosmo with Apache License 2.0 | 6 votes |
/** * Sync exceptions. * @param exceptions The exceptions. * @param masterNote The master note. */ private void syncExceptions(Map<Date, VEvent> exceptions, NoteItem masterNote) { for (Entry<Date, VEvent> entry : exceptions.entrySet()) { syncException(entry.getValue(), masterNote); } // remove old exceptions for (NoteItem noteItem : masterNote.getModifications()) { EventExceptionStamp eventException = StampUtils.getEventExceptionStamp(noteItem); if (eventException==null || !exceptions.containsKey(eventException.getRecurrenceId())) { noteItem.setIsActive(false); } } }
Example #10
Source File: StandardCalendarService.java From cosmo with Apache License 2.0 | 6 votes |
@Override public Set<Item> findEvents(CollectionItem collection, Date rangeStart, Date rangeEnd, String timeZoneId, boolean expandRecurringEvents) { Set<Item> resultSet = new HashSet<>(); String uid = collection.getUid(); if (UuidExternalGenerator.get().containsUuid(uid) || UuidSubscriptionGenerator.get().containsUuid(uid)) { NoteItemFilter filter = new NoteItemFilter(); filter.setParent(collection); EventStampFilter eventFilter = new EventStampFilter(); eventFilter.setTimeRange(rangeStart, rangeEnd); if (timeZoneId != null) { TimeZone timezone = TIMEZONE_REGISTRY.getTimeZone(timeZoneId); eventFilter.setTimezone(timezone); } filter.getStampFilters().add(eventFilter); Set<Item> externalItems = this.contentDao.findItems(filter); if (externalItems != null) { resultSet.addAll(externalItems); } } else { Set<Item> internalItems = calendarDao.findEvents(collection, rangeStart, rangeEnd, timeZoneId, expandRecurringEvents); resultSet.addAll(internalItems); } return resultSet; }
Example #11
Source File: HibBaseEventStamp.java From cosmo with Apache License 2.0 | 6 votes |
protected void setDatePropertyValue(DateProperty prop, Date date) { if (prop == null) { return; } Value value = (Value) prop.getParameters().getParameter(Parameter.VALUE); if (value != null) { prop.getParameters().remove(value); } // Add VALUE=DATE for Date values, otherwise // leave out VALUE=DATE-TIME because it is redundant if(! (date instanceof DateTime)) { prop.getParameters().add(Value.DATE); } }
Example #12
Source File: MockBaseEventStamp.java From cosmo with Apache License 2.0 | 6 votes |
/** * Sets recurrence id. * @param date The date. */ public void setRecurrenceId(Date date) { RecurrenceId recurrenceId = (RecurrenceId) getEvent().getProperties(). getProperty(Property.RECURRENCE_ID); if (date == null) { if (recurrenceId != null) { getEvent().getProperties().remove(recurrenceId); } return; } if (recurrenceId == null) { recurrenceId = new RecurrenceId(); getEvent().getProperties().add(recurrenceId); } recurrenceId.setDate(date); setDatePropertyValue(recurrenceId, date); }
Example #13
Source File: ICalendarUtilsTest.java From cosmo with Apache License 2.0 | 6 votes |
/** * Tests pin floating time. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testPinFloatingTime() throws Exception { TimeZone tz1 = TIMEZONE_REGISTRY.getTimeZone("America/Chicago"); Assert.assertEquals("20070101T000000", ICalendarUtils.pinFloatingTime(new Date("20070101"), tz1).toString()); Assert.assertEquals("20070101T000000", ICalendarUtils.pinFloatingTime(new DateTime("20070101T000000"), tz1).toString()); TimeZone tz2 = TIMEZONE_REGISTRY.getTimeZone("America/Los_Angeles"); Assert.assertEquals("20070101T000000", ICalendarUtils.pinFloatingTime(new Date("20070101"), tz1).toString()); Assert.assertEquals("20070101T000000", ICalendarUtils.pinFloatingTime(new DateTime("20070101T000000"), tz1).toString()); Assert.assertTrue(ICalendarUtils.pinFloatingTime( new Date("20070101"), tz1).before( ICalendarUtils.pinFloatingTime(new Date("20070101"), tz2))); Assert.assertTrue(ICalendarUtils.pinFloatingTime( new DateTime("20070101T000000"), tz1).before( ICalendarUtils.pinFloatingTime(new DateTime("20070101T000000"), tz2))); }
Example #14
Source File: ICalendarUtils.java From cosmo with Apache License 2.0 | 6 votes |
/** * Construct a new DateTime instance for floating times (no timezone). * If the specified date is not floating, then the instance is returned. * * This allows a floating time to be converted to an instant in time * depending on the specified timezone. * * @param date floating date * @param tz timezone * @return new DateTime instance representing floating time pinned to * the specified timezone */ public static DateTime pinFloatingTime(Date date, TimeZone tz) { try { if(date instanceof DateTime) { DateTime dt = (DateTime) date; if(dt.isUtc() || dt.getTimeZone()!=null) { return dt; } else { return new DateTime(date.toString(), tz); } } else { return new DateTime(date.toString() + "T000000", tz); } } catch (ParseException e) { throw new CosmoParseException("error parsing date", e); } }
Example #15
Source File: RecurrenceExpanderTest.java From cosmo with Apache License 2.0 | 6 votes |
/** * Tests reccurence expander floating. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testRecurrenceExpanderFloating() throws Exception { RecurrenceExpander expander = new RecurrenceExpander(); Calendar calendar = getCalendar("floating_recurring1.ics"); Date[] range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20070101T100000", range[0].toString()); Assert.assertEquals("20070119T120000", range[1].toString()); calendar = getCalendar("floating_recurring2.ics"); range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20070101T100000", range[0].toString()); Assert.assertNull(range[1]); }
Example #16
Source File: RecurrenceExpanderTest.java From cosmo with Apache License 2.0 | 6 votes |
/*** * Tests recurrence expander timezone. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testRecurrenceExpanderTimezone() throws Exception { RecurrenceExpander expander = new RecurrenceExpander(); Calendar calendar = getCalendar("tz_recurring1.ics"); Date[] range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20070101T100000", range[0].toString()); Assert.assertEquals("20070119T120000", range[1].toString()); Assert.assertEquals(((DateTime) range[0]).getTimeZone().getID(), "America/Chicago"); Assert.assertEquals(((DateTime) range[1]).getTimeZone().getID(), "America/Chicago"); calendar = getCalendar("tz_recurring2.ics"); range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20070101T100000", range[0].toString()); Assert.assertNull(range[1]); Assert.assertEquals(((DateTime) range[0]).getTimeZone().getID(), "America/Chicago"); }
Example #17
Source File: RecurrenceExpander.java From cosmo with Apache License 2.0 | 6 votes |
/** * Expand recurring event for given time-range. * @param calendar calendar containing recurring event and modifications * @param rangeStart expand start * @param rangeEnd expand end * @param timezone Optional timezone to use for floating dates. If null, the * system default is used. * @return InstanceList containing all occurences of recurring event during * time range */ public InstanceList getOcurrences(Calendar calendar, Date rangeStart, Date rangeEnd, TimeZone timezone) { ComponentList<VEvent> vevents = calendar.getComponents().getComponents(Component.VEVENT); List<Component> exceptions = new ArrayList<Component>(); Component masterComp = null; // get list of exceptions (VEVENT with RECURRENCEID) for (Iterator<VEvent> i = vevents.iterator(); i.hasNext();) { VEvent event = i.next(); if (event.getRecurrenceId() != null) { exceptions.add(event); } else { masterComp = event; } } return getOcurrences(masterComp, exceptions, rangeStart, rangeEnd, timezone); }
Example #18
Source File: MockBaseEventStamp.java From cosmo with Apache License 2.0 | 5 votes |
/** * Gets start date. * @return date. */ public Date getStartDate() { VEvent event = getEvent(); if (event==null) { return null; } DtStart dtStart = event.getStartDate(); if (dtStart == null) { return null; } return dtStart.getDate(); }
Example #19
Source File: NoteUtils.java From cosmo with Apache License 2.0 | 5 votes |
public static Date getStartDate(NoteItem note) { // start date of occurence is occurence date if (note instanceof NoteOccurrence) { return ((NoteOccurrence) note).getOccurrenceDate(); } // otherwise get start date from event stamp BaseEventStamp es = StampUtils.getBaseEventStamp(note); if (es == null) { return null; } return es.getStartDate(); }
Example #20
Source File: ICalendarUtilsTest.java From cosmo with Apache License 2.0 | 5 votes |
/** * Tests compare dates. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testCompareDates() throws Exception { TimeZone tz = TIMEZONE_REGISTRY.getTimeZone("America/Chicago"); DateTime dt = new DateTime("20070201T070000Z"); Date toTest = new Date("20070201"); Assert.assertEquals(-1, ICalendarUtils.compareDates(toTest, dt, tz)); tz = TIMEZONE_REGISTRY.getTimeZone("America/Los_Angeles"); Assert.assertEquals(1, ICalendarUtils.compareDates(toTest, dt, tz)); }
Example #21
Source File: ICalendarUtilsTest.java From cosmo with Apache License 2.0 | 5 votes |
/** * Tests convert to UTC. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testConvertToUTC() throws Exception { TimeZone tz = TIMEZONE_REGISTRY.getTimeZone("America/Chicago"); Assert.assertEquals("20070101T060000Z", ICalendarUtils.convertToUTC(new Date("20070101"), tz).toString()); Assert.assertEquals("20070101T160000Z", ICalendarUtils.convertToUTC(new DateTime("20070101T100000"), tz).toString()); tz = TIMEZONE_REGISTRY.getTimeZone("America/Los_Angeles"); Assert.assertEquals("20070101T080000Z", ICalendarUtils.convertToUTC(new Date("20070101"), tz).toString()); Assert.assertEquals("20070101T180000Z", ICalendarUtils.convertToUTC(new DateTime("20070101T100000"), tz).toString()); }
Example #22
Source File: RecurrenceExpanderTest.java From cosmo with Apache License 2.0 | 5 votes |
/** * Tests reccurence expander floating. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testRecurrenceExpanderWithExceptions() throws Exception { RecurrenceExpander expander = new RecurrenceExpander(); Calendar calendar = getCalendar("withExceptions.ics"); Date[] range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20131223", range[0].toString()); Assert.assertEquals("20140111T075100Z", range[1].toString()); }
Example #23
Source File: RecurrenceExpanderTest.java From cosmo with Apache License 2.0 | 5 votes |
/** * Tests recurrence expander long event. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testRecurrenceExpanderLongEvent() throws Exception { RecurrenceExpander expander = new RecurrenceExpander(); Calendar calendar = getCalendar("tz_recurring3.ics"); Date[] range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20070101T100000", range[0].toString()); Assert.assertEquals("20091231T120000", range[1].toString()); }
Example #24
Source File: NoteOccurrenceUtil.java From cosmo with Apache License 2.0 | 5 votes |
/** * Generate a NoteOccurrence for a given recurrence date and * master NoteItem. * @param recurrenceId * @param masterNote * @return NoteOccurrence instance */ public static NoteOccurrence createNoteOccurrence(Date recurrenceId, NoteItem masterNote) { NoteOccurrenceInvocationHandler handler = new NoteOccurrenceInvocationHandler( recurrenceId, masterNote); return (NoteOccurrence) Proxy.newProxyInstance(masterNote.getClass() .getClassLoader(), NOTE_OCCURRENCE_CLASS, handler); }
Example #25
Source File: InstanceList.java From cosmo with Apache License 2.0 | 5 votes |
/** * Adjust startRange to account for instances that begin before the given * startRange, but end after. For example if you have a daily recurring event * at 8am lasting for an hour and your startRange is 8:01am, then you * want to adjust the range back an hour to catch the instance that is * already occurring. * * @param startRange The date in range we want to adjust. * @param start The date of the event. * @param dur The duration. * @return The adjusted start Range date. */ private Date adjustStartRangeIfNecessary(Date startRange, Date start, Dur dur) { // If start is a Date, then we need to convert startRange to // a Date using the timezone present if (!(start instanceof DateTime) && timezone != null && startRange instanceof DateTime) { return ICalendarUtils.normalizeUTCDateTimeToDate( (DateTime) startRange, timezone); } // Otherwise start is a DateTime // If startRange is not the event start, no adjustment necessary if (!startRange.after(start)) { return startRange; } // Need to adjust startRange back one duration to account for instances // that occur before the startRange, but end after the startRange Dur negatedDur = dur.negate(); Calendar cal = Dates.getCalendarInstance(startRange); cal.setTime(negatedDur.getTime(startRange)); // Return new startRange only if it is before the original startRange if (cal.getTime().before(startRange)) { return org.unitedinternet.cosmo.calendar.util.Dates.getInstance(cal.getTime(), startRange); } return startRange; }
Example #26
Source File: RecurrenceExpanderTest.java From cosmo with Apache License 2.0 | 5 votes |
/** * Tests recurrence expander RDates. * @throws Exception - if something is wrong this exception is thrown. */ @Test public void testRecurrenceExpanderRDates() throws Exception { RecurrenceExpander expander = new RecurrenceExpander(); Calendar calendar = getCalendar("floating_recurring3.ics"); Date[] range = expander.calculateRecurrenceRange(calendar); Assert.assertEquals("20061212T100000", range[0].toString()); Assert.assertEquals("20101212T120000", range[1].toString()); }
Example #27
Source File: InstanceList.java From cosmo with Apache License 2.0 | 5 votes |
/** * Adjust endRange for Date instances. First convert the UTC endRange * into a Date instance, then add a second * * @param endRange The date end range. * @param start The date start. * @return The date. */ private Date adjustEndRangeIfNecessary(Date endRange, Date start) { // If instance is DateTime or timezone is not present, then // do nothing if (start instanceof DateTime || timezone == null || !(endRange instanceof DateTime)) { return endRange; } endRange = ICalendarUtils.normalizeUTCDateTimeToDefaultOffset( (DateTime) endRange, timezone); return endRange; }
Example #28
Source File: CalendarDaoImpl.java From cosmo with Apache License 2.0 | 5 votes |
@Override public Set<Item> findEvents(CollectionItem collection, Date rangeStart, Date rangeEnd, String timezoneId, boolean expandRecurringEvents) { // Create a NoteItemFilter that filters by parent NoteItemFilter itemFilter = new NoteItemFilter(); itemFilter.setParent(collection); // and EventStamp by timeRange EventStampFilter eventFilter = new EventStampFilter(); if (timezoneId != null) { TimeZone timeZone = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(timezoneId); eventFilter.setTimezone(timeZone); } eventFilter.setTimeRange(rangeStart, rangeEnd); eventFilter.setExpandRecurringEvents(expandRecurringEvents); itemFilter.getStampFilters().add(eventFilter); try { return itemFilterProcessor.processFilter(itemFilter); } catch (HibernateException e) { this.em.clear(); throw SessionFactoryUtils.convertHibernateAccessException(e); } }
Example #29
Source File: HibBaseEventStamp.java From cosmo with Apache License 2.0 | 5 votes |
public Date getRecurrenceId() { RecurrenceId rid = getEvent().getRecurrenceId(); if (rid == null) { return null; } return rid.getDate(); }
Example #30
Source File: HibBaseEventStamp.java From cosmo with Apache License 2.0 | 5 votes |
public Date getStartDate() { VEvent event = getEvent(); if(event==null) { return null; } DtStart dtStart = event.getStartDate(); if (dtStart == null) { return null; } return dtStart.getDate(); }