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

The following examples show how to use net.fortuna.ical4j.model.DateTime#isUtc() . 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: ICalendarUtils.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source File: ICalendarUtils.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Return a Date instance that represents the day that a point in
 * time translates into local time given a timezone.
 * @param utcDateTime point in time
 * @param tz timezone The timezone.
 * @return The date.
 */
public static Date normalizeUTCDateTimeToDate(DateTime utcDateTime, TimeZone tz) {
    if(!utcDateTime.isUtc()) {
        throw new IllegalArgumentException("datetime must be utc");
    }
    
    // if no timezone, use default
    if (tz == null) {
        return new Date(utcDateTime);
    }
    
    DateTime copy = (DateTime) Dates.getInstance(utcDateTime, utcDateTime);
    copy.setTimeZone(tz);
    
    try {
        return new Date(copy.toString().substring(0, 8));
    } catch (ParseException e) {
        throw new CosmoParseException("error creating Date instance", e);
    }
}
 
Example 3
Source File: ICalendarUtils.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Return a DateTime instance that is normalized according to the
 * offset of the specified timezone as compared to the default
 * system timezone.
 * 
 * @param utcDateTime point in time
 * @param tz timezone The timezone.
 * @return The date.
 */
public static Date normalizeUTCDateTimeToDefaultOffset(DateTime utcDateTime, TimeZone tz) {
    if(!utcDateTime.isUtc()) {
        throw new IllegalArgumentException("datetime must be utc");
    }
    
    // if no timezone nothing to do
    if (tz == null) {
        return utcDateTime;
    }
    
    // create copy, and set timezone
    DateTime copy = (DateTime) Dates.getInstance(utcDateTime, utcDateTime);
    copy.setTimeZone(tz);
    
    
    // Create floating instance of local time, which will give
    // us the correct offset
    try {
        return new DateTime(copy.toString());
    } catch (ParseException e) {
        throw new CosmoParseException("error creating Date instance", e);
    }
}
 
Example 4
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a TimeRangeFilter object from a DOM Element
 * @param element The DOM Element.
 * @throws ParseException - if something is wrong this exception is thrown.
 */
public TimeRangeFilter(Element element, VTimeZone timezone) throws ParseException {        
    // Get start (must be present)
    String start =
        DomUtil.getAttribute(element, ATTR_CALDAV_START, null);
    if (start == null) {
        throw new ParseException("CALDAV:comp-filter time-range requires a start time", -1);
    }
    
    DateTime trstart = new DateTime(start);
    if (! trstart.isUtc()) {
        throw new ParseException("CALDAV:param-filter timerange start must be UTC", -1);
    }

    // Get end (must be present)
    String end =
        DomUtil.getAttribute(element, ATTR_CALDAV_END, null);        
    DateTime trend = end != null ? new DateTime(end) : getDefaultEndDate(trstart);
    
    if (! trend.isUtc()) {
        throw new ParseException("CALDAV:param-filter timerange end must be UTC", -1);
    }

    setPeriod(new Period(trstart, trend));
    setTimezone(timezone);
}
 
Example 5
Source File: ICalendarUtils.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Determine if a Date is floating.  A floating Date is a Date
 * instance or a DateTime that is not utc and does not have a timezone.
 * @param date The date.
 * @return true if the date is floating, otherwise false
 */
public static boolean isFloating(Date date) {
    if(date instanceof DateTime) {
        DateTime dt = (DateTime) date;
        return !dt.isUtc() && dt.getTimeZone()==null;
    } else {
        return true;
    }
}
 
Example 6
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param dtStart The timerange start.
 * @param dtEnd The timerange end.
 */
public TimeRangeFilter(DateTime dtStart, DateTime dtEnd) {
    if (!dtStart.isUtc()) {
        throw new IllegalArgumentException("timerange start must be UTC");
    }

    if (!dtEnd.isUtc()) {
        throw new IllegalArgumentException("timerange start must be UTC");
    }

    Period period = new Period(dtStart, dtEnd);
    setPeriod(period);
}
 
Example 7
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 8
Source File: EventReloaderJob.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private org.joda.time.DateTime getDateTime(String dateType, DateTime date, Date rangeDate) {
    org.joda.time.DateTime start;
    if (date.getTimeZone() == null) {
        if (date.isUtc()) {
            log.trace("{} is without timezone, but UTC", dateType);
            start = new org.joda.time.DateTime(rangeDate, DateTimeZone.UTC).toLocalDateTime()
                    .toDateTime(CalDavLoaderImpl.defaultTimeZone);
        } else {
            log.trace("{} is without timezone, not UTC", dateType);
            start = new LocalDateTime(rangeDate).toDateTime();
        }
    } else if (DateTimeZone.getAvailableIDs().contains(date.getTimeZone().getID())) {
        log.trace("{} is with known timezone: {}", dateType, date.getTimeZone().getID());
        start = new org.joda.time.DateTime(rangeDate, DateTimeZone.forID(date.getTimeZone().getID()));
    } else {
        // unknown timezone
        log.trace("{} is with unknown timezone: {}", dateType, date.getTimeZone().getID());
        start = new org.joda.time.DateTime(rangeDate, CalDavLoaderImpl.defaultTimeZone);
    }
    return start;
}
 
Example 9
Source File: InstanceList.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Adjust a floating time if a timezone is present.  A floating time
 * is initially created with the default system timezone.  If a timezone
 * if present, we need to adjust the floating time to be in specified
 * timezone.  This allows a server in the US to return floating times for
 * a query made by someone whos timezone is in Australia.  If no timezone is
 * set for the InstanceList, then the system default timezone will be
 * used in floating time calculations.
 * <p/>
 * What happens is a floating time will get converted into a
 * date/time with a timezone.  This is ok for comparison and recurrence
 * generation purposes.  Note that Instances will get indexed as a UTC
 * date/time and for floating DateTimes, the the recurrenceId associated
 * with the Instance loses its "floating" property.
 *
 * @param date The date.
 * @return The date.
 */
private Date adjustFloatingDateIfNecessary(Date date) {
    if (timezone == null || !(date instanceof DateTime)) {
        return date;
    }

    DateTime dtDate = (DateTime) date;
    if (dtDate.isUtc() || dtDate.getTimeZone() != null) {
        return date;
    }

    try {
        return new DateTime(dtDate.toString(), timezone);
    } catch (ParseException e) {
        throw new CosmoParseException("error parsing date", e);
    }
}
 
Example 10
Source File: EventStampInterceptor.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Update the TimeRangeIndex property of the BaseEventStamp.
 * For recurring events, this means calculating the first start date
 * and the last end date for all occurences.
 */
protected HibEventTimeRangeIndex calculateEventStampIndexes(HibBaseEventStamp eventStamp) {
    Date startDate = eventStamp.getStartDate();
    Date endDate = eventStamp.getEndDate();
    
    // Handle "missing" endDate
    if(endDate==null && eventStamp instanceof HibEventExceptionStamp ) {
        // For "missing" endDate, get the duration of the master event
        // and use with the startDate of the modification to calculate
        // the endDate of the modification
        HibEventExceptionStamp exceptionStamp = (HibEventExceptionStamp) eventStamp;
        EventStamp masterStamp = exceptionStamp.getMasterStamp();
        
        // Make sure master EventStamp exists
        if(masterStamp!=null) {
            Dur duration = masterStamp.getDuration();
            if(duration!=null) {
                endDate = Dates.getInstance(duration.getTime(startDate), startDate);
            }
        }
    }
    
    
    boolean isRecurring = false;
    
    if (eventStamp.isRecurring()) {
        isRecurring = true;
        RecurrenceExpander expander = new RecurrenceExpander();
        Date[] range = expander
                .calculateRecurrenceRange(eventStamp.getEventCalendar());
        startDate = range[0];
        endDate = range[1];
    } else {
        // If there is no end date, then its a point-in-time event
        if (endDate == null) {
            endDate = startDate;
        }
    }
    
    boolean isFloating = false;
    
    // must have start date
    if(startDate==null) {
        return null;
    }
    
    // A floating date is a DateTime with no timezone, or
    // a Date
    if(startDate instanceof DateTime) {
        DateTime dtStart = (DateTime) startDate;
        if(dtStart.getTimeZone()==null && !dtStart.isUtc()) {
            isFloating = true;
        }
    } else {
        // Date instances are really floating because you can't pin
        // the a date like 20070101 to an instant without first
        // knowing the timezone
        isFloating = true;
    }
    
    HibEventTimeRangeIndex timeRangeIndex = new HibEventTimeRangeIndex();
    timeRangeIndex.setStartDate(fromDateToStringNoTimezone(startDate));
    
    
    // A null endDate equates to infinity, which is represented by
    // a String that will always come after any date when compared.
    if(endDate!=null) {
        timeRangeIndex.setEndDate(fromDateToStringNoTimezone(endDate));
    }
    else {
        timeRangeIndex.setEndDate(HibEventStamp.TIME_INFINITY);
    }
    
    timeRangeIndex.setIsFloating(isFloating);
    timeRangeIndex.setIsRecurring(isRecurring);
    
    return timeRangeIndex;
}