Java Code Examples for net.fortuna.ical4j.model.DateTime#setUtc()

The following examples show how to use net.fortuna.ical4j.model.DateTime#setUtc() . 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: EventStampInterceptor.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private 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 2
Source File: ModificationUidImpl.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: LimitRecurrenceSetTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests limit floating recurrence set.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testLimitFloatingRecurrenceSet() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "limit_recurr_float_test.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(3, calendar.getComponents().getComponents("VEVENT").size());
    
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060102T170000");
    DateTime end = new DateTime("20060104T170000");
    
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setLimit(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    Assert.assertEquals(2, filterCal.getComponents().getComponents("VEVENT").size());
    // Make sure 2nd override is dropped
    ComponentList<VEvent> vevents = filterCal.getComponents().getComponents(VEvent.VEVENT);        
    for(VEvent c : vevents) {            
        Assert.assertNotSame("event 6 changed 2",c.getProperties().getProperty("SUMMARY").getValue());
    }   
}
 
Example 4
Source File: EventResource.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * @param recurIdList
 * @param iterator  The Data in Iterator should be DateTime
 */
public void removeRecurIDListbyDateTime(ArrayList<String> recurIdList, DateList exDates) {
    String sDateTime;
    Iterator iterator = exDates.iterator();
    while (iterator.hasNext()) {
        DateTime exDate = (DateTime)iterator.next();
        exDate.setUtc(true);
        // exDates must have same type with value
        sDateTime = exDate.toString();
        recurIdList.remove(sDateTime);
    }
}
 
Example 5
Source File: EventResource.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * @param recurIdList
 * @param iterator      The Data in Iterator should be DateTime
 */
public void fillRecurIDListbyDateTime(ArrayList<String> recurIdList, DateList recurDates) {
    String sDateTime;
    Iterator iterator = recurDates.iterator();
    while (iterator.hasNext()) {
        DateTime dateTime = (DateTime)iterator.next();
        dateTime.setUtc(true);
        sDateTime = dateTime.toString();
        recurIdList.add(sDateTime);
    }
}
 
Example 6
Source File: LimitRecurrenceSetTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the set of limit recurrence.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testLimitRecurrenceSetThisAndFuture() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "limit_recurr_taf_test.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(4, calendar.getComponents().getComponents("VEVENT").size());
    
    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060108T170000", tz);
    DateTime end = new DateTime("20060109T170000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setLimit(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    Assert.assertEquals(2, filterCal.getComponents().getComponents("VEVENT").size());
    // Make sure 2nd and 3rd override are dropped
    ComponentList<VEvent> vevents = filterCal.getComponents().getComponents(VEvent.VEVENT);
    
    for(VEvent c : vevents) {            
        Assert.assertNotSame("event 6 changed",c.getProperties().getProperty("SUMMARY").getValue());
        Assert.assertNotSame("event 6 changed 2",c.getProperties().getProperty("SUMMARY").getValue());
    }   
}
 
Example 7
Source File: LimitRecurrenceSetTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests limit recurrence set.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testLimitRecurrenceSet() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "limit_recurr_test.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(5, calendar.getComponents().getComponents("VEVENT").size());
    
    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060104T010000", tz);
    DateTime end = new DateTime("20060106T010000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setLimit(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    ComponentList<CalendarComponent> comps = filterCal.getComponents();
    Assert.assertEquals(3, comps.getComponents("VEVENT").size());
    Assert.assertEquals(1, comps.getComponents("VTIMEZONE").size());
    
    // Make sure 3rd and 4th override are dropped

    ComponentList<CalendarComponent> events = comps.getComponents("VEVENT");
    for(CalendarComponent c : events) {            
        Assert.assertNotSame("event 6 changed 3",c.getProperties().getProperty("SUMMARY").getValue());
        Assert.assertNotSame("event 6 changed 4",c.getProperties().getProperty("SUMMARY").getValue());
    }
}
 
Example 8
Source File: Dates.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new date instance matching the type and timezone of the other
 * date. If no type is specified a DateTime instance is returned.
 * 
 * @param date
 *            a seed Java date instance
 * @param type
 *            the type of date instance
 * @return an instance of <code>net.fortuna.ical4j.model.Date</code>
 */
public static Date getInstance(final java.util.Date date, final Date type) {
    if (type instanceof DateTime) {
        DateTime dt = new DateTime(date);
        if (((DateTime) type).isUtc()) {
            dt.setUtc(true);
        } else {
            dt.setTimeZone(((DateTime) type).getTimeZone());
        }
        return dt;
    } else {
        return new Date(date);
    }
}
 
Example 9
Source File: Instance.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Copy a Date/DateTime and normalise to UTC if its not floating.
 * 
 * @param date The date.
 * @return The date.
 */
private Date copyNormalisedDate(Date date) {
    if (date instanceof DateTime) {
        DateTime dt = new DateTime(date);
        if (!dt.isUtc() && dt.getTimeZone() != null) {
            dt.setUtc(true);
        }
        return dt;
    } else {
        return new Date(date);
    }
}
 
Example 10
Source File: HCalendarParser.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Ical date.
 * @param original The original.
 * @return The date.
 * @throws ParseException - if something is wrong this exception is thrown.
 */
private Date icalDate(String original)
    throws ParseException {
    // in the real world, some generators use iCalendar formatted
    // dates and date-times, so try parsing those formats first before
    // going to RFC 3339 formats

    if (original.indexOf('T') == -1) {
        // date-only
        try {
            // for some reason Date's pattern matches yyyy-MM-dd, so
            // don't check it if we find -
            if (original.indexOf('-') == -1) {
                return new Date(original);
            }
        } catch (Exception e) {
            LOG.warn("", e);
        }
        //The methods parse() and format() in java.text.Format contain a design flaw 
        //that can cause one user to see another user's data.
        //moprea: making the object local will avoid thread safety issues
        SimpleDateFormat hcalDateFormat =
            new SimpleDateFormat(HCAL_DATE_PATTERN);
        return new Date(hcalDateFormat.parse(original));
    }

    // Return DateTime if we don't find '-'
    if(original.indexOf('-') == -1) {
        return new DateTime(original);
    }
    
    // otherwise try parsing RFC 3339 formats
    
    // the date-time value can represent its time zone in a few different
    // ways. we have to normalize those to match our pattern.

    String normalized = null;

    if (LOG.isDebugEnabled()) {
        LOG.debug("normalizing date-time " + original);
    }

    // 2002-10-09T19:00:00Z
    if (original.charAt(original.length()-1) == 'Z') {
        normalized = original.replace("Z", "GMT-00:00");
    }
    // 2002-10-10T00:00:00+05:00
    else if (original.indexOf("GMT") == -1 && 
             (original.charAt(original.length()-6) == '+' ||
              original.charAt(original.length()-6) == '-')) {
        String tzId = "GMT" + original.substring(original.length()-6);
        normalized = original.substring(0, original.length()-6) + tzId;
    }
    else {
        // 2002-10-10T00:00:00GMT+05:00
        normalized = original;
    }
    //The methods parse() and format() in java.text.Format contain a design flaw 
    //that can cause one user to see another user's data.
    //moprea: making the object local will avoid thread safety issues
    SimpleDateFormat hcalDateTimeFormat = new SimpleDateFormat(HCAL_DATE_TIME_PATTERN);
    DateTime dt = new DateTime(hcalDateTimeFormat.parse(normalized));

    // hCalendar does not specify a representation for timezone ids
    // or any other sort of timezone information. the best it does is
    // give us a timezone offset that we can use to convert the local
    // time to UTC. furthermore, it has no representation for floating
    // date-times. therefore, all dates are converted to UTC.

    dt.setUtc(true);

    return dt;
}
 
Example 11
Source File: ExpandRecurringEventsTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests expand event.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testExpandEvent() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "expand_recurr_test1.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(1, calendar.getComponents().getComponents("VEVENT").size());
    
    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060102T140000", tz);
    DateTime end = new DateTime("20060105T140000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setExpand(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    ComponentList<VEvent> comps = filterCal.getComponents().getComponents("VEVENT");
    
    // Should expand to 3 event components
    Assert.assertEquals(3, comps.size());
            
    Iterator<VEvent> it = comps.iterator();
    VEvent event = it.next();
    
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060102T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060102T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060103T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060103T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060104T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060104T190000Z", event.getRecurrenceId().getDate().toString());
    
    verifyExpandedCalendar(filterCal);
}
 
Example 12
Source File: ExpandRecurringEventsTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests expand event with overrides.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testExpandEventWithOverrides() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "expand_recurr_test2.ics");
    Calendar calendar = cb.build(fis);
    
    ComponentList<VEvent> comps = calendar.getComponents().getComponents("VEVENT");
    
    Assert.assertEquals(5, comps.size());
    
    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060102T140000", tz);
    DateTime end = new DateTime("20060105T140000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setExpand(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    comps = filterCal.getComponents().getComponents("VEVENT");
    
    // Should expand to 3 event components
    Assert.assertEquals(3, comps.size());
            
    Iterator<VEvent> it = comps.iterator();
    VEvent event = it.next();
    
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060102T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060102T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060103T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060103T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6 changed", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060104T210000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060104T190000Z", event.getRecurrenceId().getDate().toString());
    
    verifyExpandedCalendar(filterCal);
}
 
Example 13
Source File: ExpandRecurringEventsTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Removed test expand with non recurring event.
 * @throws Exception - if something is wrong this exception is thrown.
 */
public void removedTestExpandNonRecurringEvent() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "expand_nonrecurr_test3.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(1, calendar.getComponents().getComponents("VEVENT").size());
    
    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060102T140000", tz);
    DateTime end = new DateTime("20060105T140000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setExpand(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    ComponentList<VEvent> comps = filterCal.getComponents().getComponents("VEVENT");
    
    // Should be the same component
    Assert.assertEquals(1, comps.size());
            
    Iterator<VEvent> it = comps.iterator();
    VEvent event = it.next();
    
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060102T190000Z", event.getStartDate().getDate().toString());
    Assert.assertNull(event.getRecurrenceId());
    
    verifyExpandedCalendar(filterCal);
}
 
Example 14
Source File: InstanceListTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests all day recurring.
 *
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testAllDayRecurring() throws Exception {

    Calendar calendar = getCalendar("allday_recurring.ics");

    InstanceList instances = new InstanceList();

    // need to normalize to local timezone to get test to pass
    // in mutliple timezones
    DateTime start = new DateTime(new Date("20070101").getTime() + 1000 * 60);
    DateTime end = new DateTime(new Date("20070103").getTime() + 1000 * 60);
    start.setUtc(true);
    end.setUtc(true);

    //  This fails when run in Australia/Sydney default timezone
    //DateTime start = new DateTime("20070101T090000Z");
    //DateTime end = new DateTime("20070103T090000Z");

    addToInstanceList(calendar, instances, start, end);

    Assert.assertEquals(3, instances.size());

    Iterator<String> keys = instances.keySet().iterator();

    String key = null;
    Instance instance = null;

    key = keys.next();
    instance = (Instance) instances.get(key);

    Assert.assertEquals("20070101", key);
    Assert.assertEquals("20070101", instance.getStart().toString());
    Assert.assertEquals("20070102", instance.getEnd().toString());

    key = keys.next();
    instance = (Instance) instances.get(key);

    Assert.assertEquals("20070102", key);
    Assert.assertEquals("20070102", instance.getStart().toString());
    Assert.assertEquals("20070103", instance.getEnd().toString());

    key = keys.next();
    instance = (Instance) instances.get(key);

    Assert.assertEquals("20070103", key);
    Assert.assertEquals("20070103", instance.getStart().toString());
    Assert.assertEquals("20070104", instance.getEnd().toString());
}
 
Example 15
Source File: EventStampFilter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
private static DateTime utc(Date date) {
    DateTime dt = new DateTime(date);
    dt.setUtc(true);
    return dt;
}
 
Example 16
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
private static DateTime utc(java.util.Date date) {
    DateTime dt = new DateTime(date);
    dt.setUtc(true);
    return dt;
}
 
Example 17
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 2 votes vote down vote up
/**
 * Calculates a default end date relative to specified start date.
 * 
 * @param startDate
 * @return
 */
private DateTime getDefaultEndDate(DateTime startDate) {
    DateTime endDate = new DateTime(startDate.getTime() + TWO_YEARS_MILLIS);
    endDate.setUtc(true);
    return endDate;
}