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

The following examples show how to use net.fortuna.ical4j.model.DateTime#getTimeZone() . 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: 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 3
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 4
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 5
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 6
Source File: EntityConverter.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Gets timezone id.
 * @param date The date.
 * @return The id.
 */
private String getTzId(Date date) {
    if(date instanceof DateTime) {
        DateTime dt = (DateTime) date;
        if (dt.getTimeZone()!=null) {
            return dt.getTimeZone().getID();
        }
    }
    
    return null;
}
 
Example 7
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 8
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 9
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;
}