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

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

Introduction

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

Prototype

public static Date addYears(Date date, int amount) 

Source Link

Document

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

Usage

From source file:com.hs.mail.imap.schedule.ScheduleUtils.java

public static Date getDateBefore(String str) {
    if (str != null) {
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            char ch = str.charAt(i);
            if (!Character.isDigit(ch)) {
                try {
                    int amount = Integer.parseInt(str.substring(0, i));
                    switch (Character.toUpperCase(ch)) {
                    case 'D':
                        return DateUtils.addDays(new Date(), -amount);
                    case 'M':
                        return DateUtils.addMonths(new Date(), -amount);
                    case 'Y':
                        return DateUtils.addYears(new Date(), -amount);
                    }// w ww . ja va2 s .  co  m
                } catch (NumberFormatException e) {
                    break;
                }
            }
        }
    }
    return null;
}

From source file:net.shopxx.controller.admin.OrderStatisticController.java

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Statistic.Period period, Date beginDate, Date endDate, Model model) {
    if (period == null) {
        period = Statistic.Period.day;
    }/*from w  ww.java 2  s  .c om*/
    if (beginDate == null) {
        switch (period) {
        case year:
            beginDate = DateUtils.addYears(new Date(), -10);
            break;
        case month:
            beginDate = DateUtils.addYears(new Date(), -1);
            break;
        case day:
            beginDate = DateUtils.addMonths(new Date(), -1);
            break;
        }
    }
    if (endDate == null) {
        endDate = new Date();
    }
    model.addAttribute("periods", Statistic.Period.values());
    model.addAttribute("period", period);
    model.addAttribute("beginDate", beginDate);
    model.addAttribute("endDate", endDate);
    model.addAttribute("statistics", statisticService.analyze(period, beginDate, endDate));
    return "/admin/order_statistic/list";
}

From source file:net.shopxx.controller.admin.MemberStatisticController.java

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Statistic.Period period, Date beginDate, Date endDate, Model model) {
    if (period == null) {
        period = Statistic.Period.day;
    }/*from ww  w.ja v a 2s.com*/
    if (beginDate == null) {
        switch (period) {
        case year:
            beginDate = DateUtils.addYears(new Date(), -10);
            break;
        case month:
            beginDate = DateUtils.addYears(new Date(), -1);
            break;
        case day:
            beginDate = DateUtils.addMonths(new Date(), -1);
            break;
        }
    }
    if (endDate == null) {
        endDate = new Date();
    }
    model.addAttribute("periods", Statistic.Period.values());
    model.addAttribute("period", period);
    model.addAttribute("beginDate", beginDate);
    model.addAttribute("endDate", endDate);
    model.addAttribute("statistics", statisticService.analyze(period, beginDate, endDate));
    return "/admin/member_statistic/list";
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.DayOfQuarter.java

public Object calculate(Map<String, Object> row) throws CalculationException, PrefixAndSuffixException {
    Date d = date.getValue(row);/*from w w  w. j  av a 2  s  . c  om*/
    SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
    String s = sdf.format(d);
    if (s.compareTo("0321") < 0) {
        // 12 1 2
        if (IsLeapYear.isLeapYear(d)) {
            return 91;
        } else {
            return 90;
        }
    } else if (s.compareTo("1221") >= 0) {
        // 12 1 2
        if (IsLeapYear.isLeapYear(DateUtils.addYears(d, 1))) {
            return 91;
        } else {
            return 90;
        }
    } else if (s.compareTo("0921") >= 0) {
        // 9 10 11
        return 91;
    } else if (s.compareTo("0621") >= 0) {
        // 6 7 8
        return 92;
    } else {
        // 3 4 5
        return 92;
    }
}

From source file:com.exxonmobile.ace.hybris.core.btg.condition.operand.valueproviders.OrganizationTotalSpentInCurrencyLastYearOperandValueProvider.java

@Override
public Object getValue(final BTGOrganizationTotalSpentInCurrencyLastYearOperandModel operand,
        final UserModel user, final BTGConditionEvaluationScope evaluationScope) {
    if (user instanceof B2BCustomerModel) {
        final B2BUnitModel unit = getB2bUnitService()
                .getRootUnit(getB2bUnitService().getParent((B2BCustomerModel) user));

        Date startDateInclusive = DateUtils.addYears(new Date(), -1);
        startDateInclusive = DateUtils.setHours(startDateInclusive, 0);
        startDateInclusive = DateUtils.setMinutes(startDateInclusive, 0);
        startDateInclusive = DateUtils.setSeconds(startDateInclusive, 0);
        startDateInclusive = DateUtils.setMonths(startDateInclusive, Calendar.JANUARY);
        startDateInclusive = DateUtils.setDays(startDateInclusive, 1);

        final Date endDateNonInclusive = DateUtils.addYears(startDateInclusive, 1);

        final double total = getTotalSpentByBranch(unit, operand.getCurrency(), startDateInclusive,
                endDateNonInclusive, operand.getProductCatalogId(), operand.getCategoryCode());
        return Double.valueOf(total);
    }/*from   w w  w  . jav a 2s. c o m*/
    return Double.valueOf(0);
}

From source file:com.haulmont.timesheets.core.HolidaysCache.java

private String doLoadCache(boolean lockReadBeforeFinish) {
    try {/*from  w  w  w. j  av a 2s . com*/
        lock.writeLock().lock();
        cache = new TreeMap<>();
        LoadContext<Holiday> loadContext = new LoadContext<>(Holiday.class);
        loadContext
                .setQueryString("select e from ts$Holiday e "
                        + "where (e.startDate between :start and :end) or (e.endDate between :start and :end)")
                .setParameter("start", DateUtils.addYears(timeSource.currentTimestamp(), -1))
                .setParameter("end", DateUtils.addYears(timeSource.currentTimestamp(), 1));
        List<Holiday> holidays = dataManager.loadList(loadContext);
        for (Holiday holiday : holidays) {
            Date startDate = holiday.getStartDate();
            Date endDate = holiday.getEndDate();
            Date currentDate = startDate;
            while (currentDate.before(endDate)) {
                cache.put(currentDate, holiday);
                currentDate = DateUtils.addDays(currentDate, 1);
            }
        }

        if (lockReadBeforeFinish) {
            lock.readLock().lock();
        }
        return "Successfully loaded";
    } finally {
        lock.writeLock().unlock();
    }
}

From source file:com.audi.interview.booking.TestData.java

private void insertTestVehicles() {
    log.debug("Inserting test vehicles");
    if (vehicleService.findByLicensePlate("IN-1234") == null) {
        Vehicle testVehicle1 = new Vehicle();
        testVehicle1.setLicensePlate("IN-1234");
        testVehicle1.setModel("S4");
        testVehicle1.setColor("red");
        testVehicle1.setActive(true);//from   w  ww  .  j  a v a 2  s.c  om
        testVehicle1.setVin("BAUIEE4MZ13041220");
        testVehicle1.setValidTill(DateUtils.addYears(new Date(), 1));

        vehicleService.saveVehicle(testVehicle1);
    }

    if (vehicleService.findByLicensePlate("IN-5678") == null) {
        Vehicle testVehicle2 = new Vehicle();
        testVehicle2.setLicensePlate("IN-5678");
        testVehicle2.setModel("Q3");
        testVehicle2.setColor("black");
        testVehicle2.setActive(true);
        testVehicle2.setVin("BAUIFH4MZ13041220");
        testVehicle2.setValidTill(DateUtils.addYears(new Date(), 1));

        vehicleService.saveVehicle(testVehicle2);
    }
}

From source file:io.subutai.plugin.usergrid.rest.RestServiceImpl.java

@Override
public Response oneClick(String ename, String udom) {
    Date permanentDate = DateUtils.addYears(new Date(System.currentTimeMillis()), 10);
    final UserToken t = identityManager.createUserToken(identityManager.getActiveUser(), null, null, null, 2,
            permanentDate);/* w  w  w  .  j a  v  a  2 s . c o  m*/
    UsergridConfig localConfig = new UsergridConfig();
    localConfig.setPermanentToken(t.getFullToken());
    localConfig.setUserDomain(udom);
    localConfig.setEnvironmentName(ename);
    UUID uuid = userGridInterface.oneClickInstall(localConfig);
    OperationState waitUntilOperationFinish = waitUntilOperationFinish(uuid);
    return createResponse(uuid, waitUntilOperationFinish);
}

From source file:de.codecentric.jira.jenkins.plugin.util.TimeDifferenceHelper.java

/**
 * @param startMillis/*  www . j a va  2 s .  c om*/
 * @param endMillis
 * @return a textual (not yet fully) localized representation of the time
 *         difference
 */
public String getTimeDifferenceInWords(long startMillis, long endMillis) {
    long timeDiff = endMillis - startMillis;
    Date timestamp = new Date(startMillis);
    if (timeDiff < MINUTE) {
        // show seconds
        return formatPeriod1(timeDiff, SECOND, "Second");
    } else if (timeDiff < HOUR) {
        // show minutes and seconds
        return formatPeriod2(timeDiff, MINUTE, "Minute", SECOND, "Second");
    } else if (timeDiff < DAY) {
        // show hours and minutes
        return formatPeriod2(timeDiff, HOUR, "Hour", MINUTE, "Minute");
    } else if (timeDiff < WEEK) {
        // show days and hours
        return formatPeriod2(timeDiff, DAY, "Day", HOUR, "Hour");
    } else
    /* dateAndTime + 1 month >= currentTime */
    if (DateUtils.addMonths(timestamp, 1).after(new Date(endMillis))) {
        return formatPeriod2(timeDiff, WEEK, "Week", DAY, "Day");
    } else
    /* dateAndTime + 1 year >= currentTime */
    if (DateUtils.addYears(timestamp, 1).after(new Date(endMillis))) {
        if (DateUtils.addMonths(timestamp, 2).after(new Date(endMillis))) {
            // not more than two months, use singular
            startMillis = DateUtils.addMonths(timestamp, 1).getTime();
            timeDiff = endMillis - startMillis;
            String days = formatPeriod1(timeDiff, DAY, "Day");
            return "1 " + getPlural(1, "Month") + (!StringUtils.isEmpty(days) ? " " + days : "");
        }
        // show months and day(s)
        String monthsString = formatMonths(startMillis, endMillis);
        startMillis = DateUtils.addMonths(timestamp, countMonths(startMillis, endMillis)).getTime();
        timeDiff = endMillis - startMillis;
        String days = formatPeriod1(timeDiff, DAY, "Day");
        return monthsString + (!StringUtils.isEmpty(days) ? " " + days : "");
    } else {
        // show year and months
        if (DateUtils.addYears(timestamp, 2).after(new Date(endMillis))) {
            // not more than two years, use singular
            startMillis = DateUtils.addYears(timestamp, 1).getTime();
            String monthsString = formatMonths(startMillis, endMillis);
            return "1 " + getPlural(1, "Year") + (!StringUtils.isEmpty(monthsString) ? " " + monthsString : "");
        }
        // show years and months
        String yearString = formatYears(startMillis, endMillis);
        String monthsString = formatMonths(startMillis, endMillis);
        return yearString + (!StringUtils.isEmpty(monthsString) ? " " + monthsString : "");
    }
}

From source file:io.subutai.plugin.appscale.rest.RestServiceImpl.java

@Override
public Response oneClick(String ename, String udom) {
    LOG.info(ename + udom);//  w  w w  . j  a  v a  2  s. c  om
    if (ename != null && udom != null) {
        Date permanentDate = DateUtils.addYears(new Date(System.currentTimeMillis()), 10);
        final UserToken t = identityManager.createUserToken(identityManager.getActiveUser(), null, null, null,
                2, permanentDate);
        String token = t.getFullToken();
        AppScaleConfig appScaleConfig = new AppScaleConfig();
        appScaleConfig.setPermanentToken(token);
        appScaleConfig.setUserEnvironmentName(ename);
        appScaleConfig.setUserDomain(udom);
        LOG.info(appScaleConfig.toString());
        UUID uuid = appScaleInterface.oneClickInstall(appScaleConfig);
        OperationState op = waitUntilOperationFinish(uuid);
        return createResponse(uuid, op);
    }
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("null").build();
}