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:com.stratelia.webactiv.util.DateUtilTest.java

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

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

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

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

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

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

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

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

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

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

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

From source file:com.mingo.domain.util.DomainTestBuilder.java

/**
 * Creates date./*from ww w. j  a v  a  2 s  .com*/
 *
 * @param year the amount to add, may be negative
 * @return the new date object with the amount added
 */
public static Date createDateAndAddYear(int year) {
    return DateUtils.addYears(new Date(), year);
}

From source file:com.thoughtworks.go.security.X509CertificateGeneratorTest.java

@Test
public void shouldCreateCertsThatIsValidFromEpochToNowPlusTenYears() throws Exception {
    X509CertificateGenerator generator = new X509CertificateGenerator();
    Registration caCert = generator.createAndStoreCACertificates(keystore);
    Date epoch = new Date(0);
    X509Certificate serverCert = caCert.getFirstCertificate();
    serverCert.checkValidity(epoch); // does not throw CertificateNotYetValidException
    serverCert.checkValidity(DateUtils.addYears(new Date(), 9)); // does not throw CertificateNotYetValidException
}

From source file:com.inkubator.common.util.DateTimeUtil.java

/**
 *
 * Get or return Past or Future date based on parameter</p>
 * <p>//w w w.  j  ava2  s. c  om
 * <b>Parameter amount :</b> use negative symbol to get past date, use
 * positive symbol to get future date. Parameter amount combination with
 * parameter constantParameter. </p>
 * <p>
 * <b>Parameter constantParameter :</b> the type of times that will be
 * added. Example : CommonUtilConstant.DATE_FORMAT_MONTH</p>
 * <p>
 * <b>ConstantParameter type :</b></p>
 * <ul>
 * <li>CommonUtilConstant.DATE_FORMAT_MILLISECOND = millisecond</li>
 * <li>CommonUtilConstant.DATE_FORMAT_SECOND = second</li>
 * <li>CommonUtilConstant.DATE_FORMAT_MINUTES = minutes</li>
 * <li>CommonUtilConstant.DATE_FORMAT_HOURS = hours</li>
 * <li>CommonUtilConstant.DATE_FORMAT_DAY = day</li>
 * <li>CommonUtilConstant.DATE_FORMAT_WEEK = week</li>
 * <li>CommonUtilConstant.DATE_FORMAT_MONTH = month</li>
 * <li>CommonUtilConstant.DATE_FORMAT_YEAR = year</li>
 * </ul>
 *
 * @return Date type of past or future date
 * @param inputParam Date reference to calculate
 * @param timeDifference Integer reference, can be negative value like -7 or
 * positive value like 7
 * @param constantParameter String reference,see the CommonUtilConstant
 */
public static Date getDateFrom(Date inputParam, int timeDifference, String constantParameter) {
    Date returnDate = null;
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_DAY)) {
        returnDate = DateUtils.addDays(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_HOURS)) {
        returnDate = DateUtils.addHours(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_MILLISECOND)) {
        returnDate = DateUtils.addMilliseconds(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_MINUTES)) {
        returnDate = DateUtils.addMinutes(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_MONTH)) {
        returnDate = DateUtils.addMonths(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_SECOND)) {
        returnDate = DateUtils.addSeconds(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_YEAR)) {
        returnDate = DateUtils.addYears(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_WEEK)) {
        returnDate = DateUtils.addWeeks(inputParam, timeDifference);
    }
    return returnDate;
}

From source file:com.epam.ta.reportportal.core.job.ExpireNotUsedAccountsJobTest.java

@Before
public void prepareAccount() {
    User demoUser = new User();
    String random = RandomStringUtils.randomAlphabetic(5);
    demoUser.setLogin(random);//from  www  . j a v  a 2  s.  c o  m
    demoUser.setPassword(random);
    demoUser.setType(UserType.UPSA);
    demoUser.setIsExpired(false);
    demoUser.setEmail(random + "epam.com");

    // YEAR Before
    demoUser.getMetaInfo().setLastLogin(DateUtils.addYears(Calendar.getInstance().getTime(), -1));
    demoUser.setRole(UserRole.ADMINISTRATOR);
    userRepository.save(demoUser);
}

From source file:eu.ggnet.dwoss.report.assist.ReportUtil.java

/**
 * Returns all Lines of the Report for Category Invoiced, split by mfgDate - startOfReport &lt; 1 year and the rest.
 * This consists of://from ww w  . j  a v  a  2s. com
 * <ul>
 * <li>Position of Type Invoice, with no References</li>
 * <li>Position of Type UNIT_ANNEX in DocumentType CREDIT_MEMO/ANNULATIION_INVOICE and a Referencing Invoice in the same report.</li>
 * </ul>
 * <p>
 * It's not allowed to have a null value in the collection.
 * <p>
 * @param lines
 * @param startingDate
 * @return all Lines of the Report for Category Invoiced.
 */
public static YearSplit filterInvoicedSplit(Collection<ReportLine> lines, Date startingDate) {
    NavigableSet<ReportLine> pastSplit = new TreeSet<>();
    NavigableSet<ReportLine> preSplit = new TreeSet<>();
    Date splitter = DateUtils.addYears(startingDate, -1);
    for (ReportLine line : filterInvoiced(lines)) {
        if (splitter.before(line.getMfgDate())) {
            preSplit.add(line);
        } else {
            pastSplit.add(line);
        }
    }
    return new YearSplit(startingDate, preSplit, pastSplit);
}

From source file:com.thoughtworks.go.security.X509CertificateGeneratorTest.java

@Test
public void shouldCreateCertsForAgentThatIsValidFromEpochToNowPlusTenYears() throws Exception {
    X509CertificateGenerator generator = new X509CertificateGenerator();
    Registration agentCertChain = generator.createAgentCertificate(keystore, "agentHostName");
    Date epoch = new Date(0);
    X509Certificate agentCert = agentCertChain.getFirstCertificate();
    agentCert.checkValidity(epoch); // does not throw CertificateNotYetValidException
    agentCert.checkValidity(DateUtils.addYears(new Date(), 9)); // does not throw CertificateNotYetValidException
}

From source file:com.progym.custom.fragments.FoodCalloriesProgressYearlyLineFragment.java

@Click
void ivPrevYear() {
    ivNextYear.startAnimation(fadeIn);
    rlRootGraphLayout.startAnimation(fadeOut);
    DATE = DateUtils.addYears(DATE, -1);
    setYearProgressData(DATE);
}

From source file:com.progym.custom.fragments.FoodCalloriesProgressYearlyLineFragment.java

@Click
void ivNextYear() {
    ivNextYear.startAnimation(fadeIn);
    rlRootGraphLayout.startAnimation(fadeOut);
    DATE = DateUtils.addYears(DATE, 1);
    setYearProgressData(DATE);
}

From source file:com.progym.custom.fragments.FoodProgressYearlyLineFragment.java

@Click
void ivPrevYear() {
    ivPrevYear.startAnimation(fadeIn);// w  w w . j  a v  a  2  s. com

    rightOut.setDuration(300);
    rightOut.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            DATE = DateUtils.addYears(DATE, -1);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setYearProgressData(DATE, true);
        }
    });
    viewChart.startAnimation(rightOut);

}