Example usage for org.apache.commons.lang3.time DateUtils addYears

List of usage examples for org.apache.commons.lang3.time DateUtils addYears

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time DateUtils addYears.

Prototype

public static Date addYears(final Date date, final int amount) 

Source Link

Document

Adds a number of years to a date returning a new object.

Usage

From source file:org.kuali.kra.protocol.actions.ActionHelperBase.java

/**
 * Builds an expiration date, defaulting to the expiration date from the protocol.  
 * //from w ww  .  j  a v  a 2s.c  o  m
 * If the expiration date from the protocol is null, or if the protocol is new or a renewal, creates an expiration date exactly one year ahead and one day 
 * less than the approval date.
 * 
 * @param protocol
 * @param approvalDate
 * @return a non-null expiration date
 */
protected Date buildExpirationDate(ProtocolBase protocol, Date approvalDate) {
    Date expirationDate = protocol.getExpirationDate();

    if (expirationDate == null || protocol.isNew() || protocol.isRenewal()) {
        java.util.Date newExpirationDate = DateUtils.addYears(approvalDate,
                getDefaultExpirationDateDifference());
        newExpirationDate = DateUtils.addDays(newExpirationDate, -1);
        expirationDate = org.kuali.coeus.sys.framework.util.DateUtils.convertToSqlDate(newExpirationDate);
    }

    return expirationDate;
}

From source file:org.openmrs.ISO8601Duration.java

/**
 * Add this duration to given startDate//from   w w  w .j a  v  a  2s  .  co m
 * 
 * @param startDate
 * @param frequency is used to calculate time to be added to startDate when duration unit is
 *            'Recurring Interval'
 * @return date which is startDate plus duration
 */
public Date addToDate(Date startDate, OrderFrequency frequency) {
    if (SECONDS_CODE.equals(code))
        return DateUtils.addSeconds(startDate, duration);
    if (MINUTES_CODE.equals(code))
        return DateUtils.addMinutes(startDate, duration);
    if (HOURS_CODE.equals(code))
        return DateUtils.addHours(startDate, duration);
    if (DAYS_CODE.equals(code))
        return DateUtils.addDays(startDate, duration);
    if (WEEKS_CODE.equals(code))
        return DateUtils.addWeeks(startDate, duration);
    if (MONTHS_CODE.equals(code))
        return DateUtils.addMonths(startDate, duration);
    if (YEARS_CODE.equals(code))
        return DateUtils.addYears(startDate, duration);
    if (RECURRING_INTERVAL_CODE.equals(code)) {
        if (frequency == null)
            throw new APIException("Frequency can not be null when duration in Recurring Interval");
        return DateUtils.addSeconds(startDate,
                (int) (duration * SECONDS_PER_DAY / frequency.getFrequencyPerDay()));
    }
    throw new APIException(String.format("Unknown code '%s' for ISO8601 duration units", code));
}

From source file:org.silverpeas.core.date.AbstractDateDatable.java

@Override
public T addYears(final int amount) {
    return newInstanceFrom(DateUtils.addYears(this, amount));
}

From source file:org.silverpeas.core.util.DateUtilTest.java

@Test
public void testGetOutputDateAndHour() {
    Date date = DateUtil.resetHour(java.sql.Date.valueOf("2013-05-21"));
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("05/21/2013"));

    Date year = DateUtils.addYears(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(year, LANGUAGE), is("05/21/2014"));

    Date month = DateUtils.addMonths(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(month, LANGUAGE), is("06/21/2013"));

    Date day = DateUtils.addDays(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(day, LANGUAGE), is("05/22/2013"));

    Date hour = DateUtils.addHours(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("05/21/2013 01:00"));
    hour = DateUtils.addHours(date, 12);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("05/21/2013 12:00"));
    hour = DateUtils.addHours(date, 22);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("05/21/2013 22:00"));

    Date minute = DateUtils.addMinutes(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("05/21/2013 00:01"));
    minute = DateUtils.addMinutes(date, 59);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("05/21/2013 00:59"));
    minute = DateUtils.addMinutes(date, 60);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("05/21/2013 01:00"));
    minute = DateUtils.addMinutes(date, 61);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("05/21/2013 01:01"));

    Date second = DateUtils.addSeconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("05/21/2013 00:00"));
    second = DateUtils.addSeconds(date, 59);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("05/21/2013 00:00"));
    second = DateUtils.addSeconds(date, 60);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("05/21/2013 00:01"));
    second = DateUtils.addSeconds(date, 61);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("05/21/2013 00:01"));

    Date millisecond = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("05/21/2013 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 999);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("05/21/2013 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 1000);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("05/21/2013 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 1001);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("05/21/2013 00:00"));

    // 2013-05-21 23:59:59.999
    date = DateUtils.addHours(//  w w w .  j a va  2 s.c  o  m
            DateUtils.addMinutes(DateUtils.addSeconds(DateUtils.addMilliseconds(date, 999), 59), 59), 23);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("05/21/2013 23:59"));

    // 2013-05-22 00:00:00.000
    date = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("05/22/2013"));

    // 2013-05-22 00:00:00.001
    date = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("05/22/2013 00:00"));
}

From source file:org.silverpeas.core.web.calendar.CalendarTimeWindowViewContext.java

/**
 * Centralization.//from ww w  . j  a va  2 s . co m
 * @param offset
 */
private void moveReferenceDate(int offset) {
    switch (viewType) {
    case YEARLY:
        setReferenceDay(DateUtils.addYears(referenceDay.getDate(), offset), offset);
        break;
    case MONTHLY:
        setReferenceDay(DateUtils.addMonths(referenceDay.getDate(), offset), offset);
        break;
    case WEEKLY:
        setReferenceDay(DateUtils.addWeeks(referenceDay.getDate(), offset), offset);
        break;
    case DAILY:
        setReferenceDay(DateUtils.addDays(referenceDay.getDate(), offset), offset);
        break;
    }
}

From source file:org.silverpeas.termsOfService.constant.TermsOfServiceAcceptanceFrequencyTest.java

@Test
public void testIsAcceptanceDateExpired() {

    // This test is to don't forget to add or remove test block below in case of upgrade of the
    // enum./* w  w  w  .j a  v a 2s. c o  m*/
    assertThat(values().length, is(7));

    // NEVER
    assertThat(NEVER.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(false));
    assertThat(NEVER.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(false));
    assertThat(NEVER.isAcceptanceDateExpired(FIRST_OF_JUNE, java.sql.Date.valueOf("1970-01-01"), LOCALE),
            is(false));

    // ALWAYS
    assertThat(ALWAYS.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(true));
    assertThat(ALWAYS.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(true));
    assertThat(ALWAYS.isAcceptanceDateExpired(FIRST_OF_JUNE, java.sql.Date.valueOf("1970-01-01"), LOCALE),
            is(true));

    // ONE
    assertThat(ONE.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(true));
    assertThat(ONE.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(false));
    assertThat(ONE.isAcceptanceDateExpired(FIRST_OF_JUNE, java.sql.Date.valueOf("1970-01-01"), LOCALE),
            is(false));

    // DAILY
    assertThat(DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(true));
    assertThat(DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(false));
    assertThat(
            DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMilliseconds(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addDays(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addWeeks(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMonths(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addYears(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMilliseconds(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(DAILY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addWeeks(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMonths(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(DAILY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addYears(LAST_OF_JUNE, -1), LOCALE),
            is(true));

    // WEEKLY
    assertThat(WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(true));
    assertThat(WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(false));
    assertThat(
            WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMilliseconds(FIRST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addDays(FIRST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addWeeks(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMonths(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(WEEKLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addYears(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(
            WEEKLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMilliseconds(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(WEEKLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(WEEKLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -6), LOCALE),
            is(false));
    assertThat(WEEKLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addWeeks(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(WEEKLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMonths(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(WEEKLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addYears(LAST_OF_JUNE, -1), LOCALE),
            is(true));

    // MONTHLY
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(false));
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMilliseconds(FIRST_OF_JUNE, -1),
            LOCALE), is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addDays(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addWeeks(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMonths(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addYears(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(
            MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMilliseconds(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -29), LOCALE),
            is(false));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addWeeks(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addWeeks(LAST_OF_JUNE, -4), LOCALE),
            is(false));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addWeeks(LAST_OF_JUNE, -5), LOCALE),
            is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMonths(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(MONTHLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addYears(LAST_OF_JUNE, -1), LOCALE),
            is(true));

    // YEARLY
    assertThat(YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, null, LOCALE), is(true));
    assertThat(YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, FIRST_OF_JUNE, LOCALE), is(false));
    assertThat(
            YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMilliseconds(FIRST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addDays(FIRST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addWeeks(FIRST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addMonths(FIRST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(FIRST_OF_JUNE, DateUtils.addYears(FIRST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(
            YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMilliseconds(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addDays(LAST_OF_JUNE, -150), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addWeeks(LAST_OF_JUNE, -25), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMonths(LAST_OF_JUNE, -1), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addMonths(LAST_OF_JUNE, -5), LOCALE),
            is(false));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addYears(LAST_OF_JUNE, -1), LOCALE),
            is(true));
    assertThat(YEARLY.isAcceptanceDateExpired(LAST_OF_JUNE, DateUtils.addYears(LAST_OF_JUNE, 1), LOCALE),
            is(true));
}

From source file:storybook.ui.chart.jfreechart.ChartUtil.java

@SuppressWarnings("unchecked")
public static TreeSet<Date> correctDates(TreeSet<Date> paramTreeSet) {
    TreeSet localTreeSet = new TreeSet();
    Date localDate1 = (Date) paramTreeSet.first();
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.setTime(localDate1);//  ww w .j av a  2  s.  c om
    int i = localCalendar1.get(Calendar.YEAR);
    if (i > 1900) {
        return paramTreeSet;
    }
    for (Date localDate2 : paramTreeSet) {
        Calendar localCalendar2 = Calendar.getInstance();
        localCalendar2.setTime(localDate2);
        int j = localCalendar2.get(Calendar.YEAR);
        localDate2 = DateUtils.addYears(localDate2, 1900 - j);
        localTreeSet.add(localDate2);
    }
    return localTreeSet;
}

From source file:ubic.basecode.util.DateUtil.java

/**
 * Turn a string like '-7d' into the date equivalent to "seven days ago". Supports 'd' for day, 'h' for hour, 'm'
 * for minutes, "M" for months and "y" for years. Start with a '-' to indicate times in the past ('+' is not
 * necessary for future). Values must be integers.
 * //from w w  w  . j ava2  s.  co  m
 * @param date to be added/subtracted to
 * @param dateString
 * @author Paul Pavlidis
 * @return Date relative to 'now' as modified by the input date string.
 */
public static Date getRelativeDate(Date date, String dateString) {

    if (date == null)
        throw new IllegalArgumentException("Null date");

    Pattern pat = Pattern.compile("([+-]?[0-9]+)([dmhMy])");

    Matcher match = pat.matcher(dateString);
    boolean matches = match.matches();

    if (!matches) {
        throw new IllegalArgumentException(
                "Couldn't make sense of " + dateString + ", please use something like -7d or -8h");
    }

    int amount = Integer.parseInt(match.group(1).replace("+", ""));
    String unit = match.group(2);

    if (unit.equals("h")) {
        return DateUtils.addHours(date, amount);
    } else if (unit.equals("m")) {
        return DateUtils.addMinutes(date, amount);
    } else if (unit.equals("d")) {
        return DateUtils.addDays(date, amount);
    } else if (unit.equals("y")) {
        return DateUtils.addYears(date, amount);
    } else if (unit.equals("M")) {
        return DateUtils.addMonths(date, amount);
    } else {
        throw new IllegalArgumentException(
                "Couldn't make sense of units in " + dateString + ", please use something like -7d or -8h");
    }

}

From source file:ubic.basecode.util.DateUtil.java

/**
 * Compute the number of seconds spanned by the given dates. If no or a single date is provided, returns 0.
 * // w  ww .j  av  a  2s . c  o  m
 * @param dates
 * @return
 */
public static long numberOfSecondsBetweenDates(Collection<Date> dates) {
    if (dates == null)
        throw new IllegalArgumentException();
    if (dates.size() < 2)
        return 0;
    // dates we are sure are safe...
    Date max = DateUtils.addYears(new Date(), -500);
    Date min = DateUtils.addYears(new Date(), 500);
    for (Date d : dates) {
        if (d.before(min)) {
            min = d;
        }

        if (d.after(max)) {
            max = d;
        }
    }
    return Math.round((max.getTime() - min.getTime()) / 1000.00);
}

From source file:ubic.basecode.util.DateUtilTest.java

public void testgetRelative5yearsago() {

    Date now = new Date();

    Date expectedValue = DateUtils.addYears(now, -5);
    Date actualValue = DateUtil.getRelativeDate(now, "-5y");

    assertEquals(expectedValue, actualValue);

}