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

The following examples show how to use net.fortuna.ical4j.model.DateTime#setTimeZone() . 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: Calendar_EventRepeatMaster.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 获取重复时间表达式中结束的日期
 * @return
 */
public Date getRecurrenceEndDate() {
	final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry()
			.getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());
	if (recurrenceRule != null) {
		try {
			final Recur recur = new Recur(recurrenceRule);
			final Date dUntil = recur.getUntil();
			final DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
			if (dtUntil != null) {
				dtUntil.setTimeZone(tz);
				return dtUntil;
			}
		} catch (final ParseException e) {
			System.out.println("cannot restore recurrence rule");
			e.printStackTrace();
		}
	}
	return null;
}
 
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: CalendarEntry.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * @param rule
 * @return date of recurrence end
 */
public Date getRecurrenceEndDate() {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrenceRule != null) {
        try {
            final Recur recur = new Recur(recurrenceRule);
            final Date dUntil = recur.getUntil();
            final DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
            if (dtUntil != null) {
                dtUntil.setTimeZone(tz);
                return dtUntil;
            }
        } catch (final ParseException e) {
            log.error("cannot restore recurrence rule", e);
        }
    }

    return null;
}
 
Example 5
Source File: CalendarEntry.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * @param rule
 * @return date of recurrence end
 */
public Date getRecurrenceEndDate() {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrenceRule != null) {
        try {
            final Recur recur = new Recur(recurrenceRule);
            final Date dUntil = recur.getUntil();
            final DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
            if (dtUntil != null) {
                dtUntil.setTimeZone(tz);
                return dtUntil;
            }
        } catch (final ParseException e) {
            log.error("cannot restore recurrence rule", e);
        }
    }

    return null;
}
 
Example 6
Source File: ICalUtils.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
static DateTime toDateTime(ZonedDateTime zonedDateTime, TimeZoneRegistry registry) {
    final String tzId = zonedDateTime.getZone().getId();
    final TimeZone timezone = registry.getTimeZone(tzId.equals("Z") ? "UTC" : tzId);
    // workaround for https://github.com/apache/camel-quarkus/issues/838
    final DateTime result = new DateTime();
    result.setTimeZone(timezone);
    result.setTime(zonedDateTime.toInstant().toEpochMilli());
    // To reproduce https://github.com/apache/camel-quarkus/issues/838 comment the above, enable the following
    // and remove the TZ from DTSTART and DTEND in src/test/resources/test.ics
    // final DateTime result = new DateTime(zonedDateTime.toInstant().toEpochMilli());
    return result;
}
 
Example 7
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 8
Source File: CalendarUtils.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Build iCalendar-compliant recurrence rule
 * 
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
        // create recurrence rule
        final StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(CalendarEntry.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(CalendarEntry.DAILY);
            sb.append(";");
            sb.append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(CalendarEntry.WEEKLY);
            sb.append(";");
            sb.append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
            recurEndDT.setTimeZone(tz);
            sb.append(";");
            sb.append(CalendarEntry.UNTIL);
            sb.append("=");
            sb.append(recurEndDT.toString());
        }
        try {
            final Recur recur = new Recur(sb.toString());
            final RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (final ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }

    return null;
}
 
Example 9
Source File: CalendarUtils.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Build iCalendar-compliant recurrence rule
 * 
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
        // create recurrence rule
        final StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(CalendarEntry.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(CalendarEntry.DAILY);
            sb.append(";");
            sb.append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(CalendarEntry.WEEKLY);
            sb.append(";");
            sb.append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
            recurEndDT.setTimeZone(tz);
            sb.append(";");
            sb.append(CalendarEntry.UNTIL);
            sb.append("=");
            sb.append(recurEndDT.toString());
        }
        try {
            final Recur recur = new Recur(sb.toString());
            final RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (final ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }

    return null;
}