Example usage for org.joda.time MutableDateTime addYears

List of usage examples for org.joda.time MutableDateTime addYears

Introduction

In this page you can find the example usage for org.joda.time MutableDateTime addYears.

Prototype

public void addYears(final int years) 

Source Link

Document

Add a number of years to the date.

Usage

From source file:com.tmathmeyer.sentinel.ui.views.year.YearCalendar.java

License:Open Source License

@Override
public void previous() {
    MutableDateTime mdt = new MutableDateTime(this.calendarStart);
    mdt.addYears(-2);
    this.display(mdt.toDateTime());
}

From source file:controllers.api.DashboardsApiController.java

License:Open Source License

private void nextStep(String interval, MutableDateTime currentTime) {
    switch (interval) {
    case "minute":
        currentTime.addMinutes(1);/*from   www . j  a  va  2 s.  c o  m*/
        break;
    case "hour":
        currentTime.addHours(1);
        break;
    case "day":
        currentTime.addDays(1);
        break;
    case "week":
        currentTime.addWeeks(1);
        break;
    case "month":
        currentTime.addMonths(1);
        break;
    case "quarter":
        currentTime.addMonths(3);
        break;
    case "year":
        currentTime.addYears(1);
        break;
    default:
        throw new IllegalArgumentException("Invalid duration specified: " + interval);
    }
}

From source file:org.codelibs.elasticsearch.common.joda.DateMathParser.java

License:Apache License

private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone)
        throws ElasticsearchParseException {
    if (timeZone == null) {
        timeZone = DateTimeZone.UTC;/*from  w ww  .j  a v  a 2  s .c  om*/
    }
    MutableDateTime dateTime = new MutableDateTime(time, timeZone);
    for (int i = 0; i < mathString.length();) {
        char c = mathString.charAt(i++);
        final boolean round;
        final int sign;
        if (c == '/') {
            round = true;
            sign = 1;
        } else {
            round = false;
            if (c == '+') {
                sign = 1;
            } else if (c == '-') {
                sign = -1;
            } else {
                throw new ElasticsearchParseException("operator not supported for date math [{}]", mathString);
            }
        }

        if (i >= mathString.length()) {
            throw new ElasticsearchParseException("truncated date math [{}]", mathString);
        }

        final int num;
        if (!Character.isDigit(mathString.charAt(i))) {
            num = 1;
        } else {
            int numFrom = i;
            while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) {
                i++;
            }
            if (i >= mathString.length()) {
                throw new ElasticsearchParseException("truncated date math [{}]", mathString);
            }
            num = Integer.parseInt(mathString.substring(numFrom, i));
        }
        if (round) {
            if (num != 1) {
                throw new ElasticsearchParseException("rounding `/` can only be used on single unit types [{}]",
                        mathString);
            }
        }
        char unit = mathString.charAt(i++);
        MutableDateTime.Property propertyToRound = null;
        switch (unit) {
        case 'y':
            if (round) {
                propertyToRound = dateTime.yearOfCentury();
            } else {
                dateTime.addYears(sign * num);
            }
            break;
        case 'M':
            if (round) {
                propertyToRound = dateTime.monthOfYear();
            } else {
                dateTime.addMonths(sign * num);
            }
            break;
        case 'w':
            if (round) {
                propertyToRound = dateTime.weekOfWeekyear();
            } else {
                dateTime.addWeeks(sign * num);
            }
            break;
        case 'd':
            if (round) {
                propertyToRound = dateTime.dayOfMonth();
            } else {
                dateTime.addDays(sign * num);
            }
            break;
        case 'h':
        case 'H':
            if (round) {
                propertyToRound = dateTime.hourOfDay();
            } else {
                dateTime.addHours(sign * num);
            }
            break;
        case 'm':
            if (round) {
                propertyToRound = dateTime.minuteOfHour();
            } else {
                dateTime.addMinutes(sign * num);
            }
            break;
        case 's':
            if (round) {
                propertyToRound = dateTime.secondOfMinute();
            } else {
                dateTime.addSeconds(sign * num);
            }
            break;
        default:
            throw new ElasticsearchParseException("unit [{}] not supported for date math [{}]", unit,
                    mathString);
        }
        if (propertyToRound != null) {
            if (roundUp) {
                // we want to go up to the next whole value, even if we are already on a rounded value
                propertyToRound.add(1);
                propertyToRound.roundFloor();
                dateTime.addMillis(-1); // subtract 1 millisecond to get the largest inclusive value
            } else {
                propertyToRound.roundFloor();
            }
        }
    }
    return dateTime.getMillis();
}

From source file:org.elasticsearch.common.joda.DateMathParser.java

License:Apache License

private long parseMath(String mathString, long time, boolean roundUp) throws ElasticsearchParseException {
    MutableDateTime dateTime = new MutableDateTime(time, DateTimeZone.UTC);
    try {//from   w  ww . j av  a  2s  .com
        for (int i = 0; i < mathString.length();) {
            char c = mathString.charAt(i++);
            int type;
            if (c == '/') {
                type = 0;
            } else if (c == '+') {
                type = 1;
            } else if (c == '-') {
                type = 2;
            } else {
                throw new ElasticsearchParseException(
                        "operator not supported for date math [" + mathString + "]");
            }

            int num;
            if (!Character.isDigit(mathString.charAt(i))) {
                num = 1;
            } else {
                int numFrom = i;
                while (Character.isDigit(mathString.charAt(i))) {
                    i++;
                }
                num = Integer.parseInt(mathString.substring(numFrom, i));
            }
            if (type == 0) {
                // rounding is only allowed on whole numbers
                if (num != 1) {
                    throw new ElasticsearchParseException(
                            "rounding `/` can only be used on single unit types [" + mathString + "]");
                }
            }
            char unit = mathString.charAt(i++);
            switch (unit) {
            case 'y':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.yearOfCentury().roundCeiling();
                    } else {
                        dateTime.yearOfCentury().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addYears(num);
                } else if (type == 2) {
                    dateTime.addYears(-num);
                }
                break;
            case 'M':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.monthOfYear().roundCeiling();
                    } else {
                        dateTime.monthOfYear().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addMonths(num);
                } else if (type == 2) {
                    dateTime.addMonths(-num);
                }
                break;
            case 'w':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.weekOfWeekyear().roundCeiling();
                    } else {
                        dateTime.weekOfWeekyear().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addWeeks(num);
                } else if (type == 2) {
                    dateTime.addWeeks(-num);
                }
                break;
            case 'd':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.dayOfMonth().roundCeiling();
                    } else {
                        dateTime.dayOfMonth().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addDays(num);
                } else if (type == 2) {
                    dateTime.addDays(-num);
                }
                break;
            case 'h':
            case 'H':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.hourOfDay().roundCeiling();
                    } else {
                        dateTime.hourOfDay().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addHours(num);
                } else if (type == 2) {
                    dateTime.addHours(-num);
                }
                break;
            case 'm':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.minuteOfHour().roundCeiling();
                    } else {
                        dateTime.minuteOfHour().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addMinutes(num);
                } else if (type == 2) {
                    dateTime.addMinutes(-num);
                }
                break;
            case 's':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.secondOfMinute().roundCeiling();
                    } else {
                        dateTime.secondOfMinute().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addSeconds(num);
                } else if (type == 2) {
                    dateTime.addSeconds(-num);
                }
                break;
            default:
                throw new ElasticsearchParseException(
                        "unit [" + unit + "] not supported for date math [" + mathString + "]");
            }
        }
    } catch (Exception e) {
        if (e instanceof ElasticsearchParseException) {
            throw (ElasticsearchParseException) e;
        }
        throw new ElasticsearchParseException("failed to parse date math [" + mathString + "]");
    }
    return dateTime.getMillis();
}

From source file:org.mifos.ui.core.controller.SavingsProductFormValidator.java

License:Open Source License

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
@Override//  w  ww. j  a  v  a2s. c  om
public void validate(Object target, Errors errors) {

    SavingsProductFormBean formBean = (SavingsProductFormBean) target;

    if (formBean.getGeneralDetails().getName().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.name");
    }
    if (formBean.getGeneralDetails().getDescription().length() > 200) {
        errors.reject("Size.generalDetails.description");
    }

    if (formBean.getGeneralDetails().getShortName().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.shortName");
    }

    if (formBean.getGeneralDetails().getSelectedCategory().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.selectedCategory");
    }

    if (null == formBean.getGeneralDetails().getStartDateDay()
            || 1 > formBean.getGeneralDetails().getStartDateDay()
            || 31 < formBean.getGeneralDetails().getStartDateDay()) {
        errors.reject("Min.generalDetails.startDateDay");
    }

    if (null == formBean.getGeneralDetails().getStartDateMonth()
            || 1 > formBean.getGeneralDetails().getStartDateMonth()
            || 12 < formBean.getGeneralDetails().getStartDateMonth()) {
        errors.reject("Min.generalDetails.startDateMonth");
    }

    if (null == formBean.getGeneralDetails().getStartDateAsDateTime()
            || !(formBean.getGeneralDetails().getStartDateYear().length() == 4)) {
        errors.reject("Min.generalDetails.startDate");
    } else {
        MutableDateTime nextYear = new MutableDateTime();
        nextYear.setDate(new Date().getTime());
        nextYear.addYears(1);

        if (formBean.getGeneralDetails().getStartDateAsDateTime() != null
                && formBean.getGeneralDetails().getStartDateAsDateTime().compareTo(nextYear) > 0) {
            errors.reject("Min.generalDetails.startDate");
        }
    }

    if ((null != formBean.getGeneralDetails().getEndDateDay()
            || null != formBean.getGeneralDetails().getEndDateMonth()
            || null != formBean.getGeneralDetails().getEndDateMonth())
            && formBean.getGeneralDetails().getEndDateAsDateTime() == null) {
        errors.reject("Min.generalDetails.endDate");
    }

    if (formBean.getGeneralDetails().getStartDateAsDateTime() != null
            && formBean.getGeneralDetails().getEndDateAsDateTime() != null
            && formBean.getGeneralDetails().getStartDateAsDateTime()
                    .compareTo(formBean.getGeneralDetails().getEndDateAsDateTime()) > 0) {
        errors.reject("Min.generalDetails.endDate");
    }

    if (formBean.getGeneralDetails().getSelectedApplicableFor().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.selectedApplicableFor");
    }

    if (formBean.getSelectedDepositType().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedDepositType");
    } else if (formBean.isMandatory()
            && (null == formBean.getAmountForDeposit() || formBean.getAmountForDeposit() <= 0)) {
        errors.reject("Min.savingsProduct.amountForDesposit");
    }

    if (formBean.isGroupSavingAccount() && formBean.getSelectedGroupSavingsApproach().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedGroupSavingsApproach");
    }

    if (!formBean.isInterestRateZero()) {
        if (null == formBean.getInterestRate() || formBean.getInterestRate().doubleValue() < 1.0
                || formBean.getInterestRate().doubleValue() > 100.0
                || errorProcessor.getRejectedValue("interestRate") != null) {
            if (errorProcessor.getTarget() == null) {
                errors.reject("NotNull.savingsProduct.interestRate");
            } else {
                BindException bindException = new BindException(errorProcessor.getTarget(),
                        errorProcessor.getObjectName());
                bindException.addError(new FieldError(errorProcessor.getObjectName(), "interestRate",
                        errorProcessor.getRejectedValue("interestRate"), true,
                        new String[] { "NotNull.savingsProduct.interestRate" }, null, null));
                errors.addAllErrors(bindException);
            }
        }

        if (formBean.getSelectedInterestCalculation().trim().isEmpty()) {
            errors.reject("NotEmpty.savingsProduct.selectedInterestCalculation");
        }

        if (null == formBean.getInterestCalculationFrequency() || formBean.getInterestCalculationFrequency() < 1
                || errorProcessor.getRejectedValue("interestCalculationFrequency") != null) {
            if (errorProcessor.getTarget() == null) {
                errors.reject("NotNull.savingsProduct.interestCalculationFrequency");
            } else {
                BindException bindException = new BindException(errorProcessor.getTarget(),
                        errorProcessor.getObjectName());
                bindException.addError(new FieldError(errorProcessor.getObjectName(),
                        "interestCalculationFrequency",
                        errorProcessor.getRejectedValue("interestCalculationFrequency"), true,
                        new String[] { "NotNull.savingsProduct.interestCalculationFrequency" }, null, null));
                errors.addAllErrors(bindException);
            }
        }

        if (null == formBean.getInterestPostingMonthlyFrequency()
                || formBean.getInterestPostingMonthlyFrequency() < 1
                || errorProcessor.getRejectedValue("interestPostingMonthlyFrequency") != null) {
            if (errorProcessor.getTarget() == null) {
                errors.reject("Min.savingsProduct.interestPostingMonthlyFrequency");
            } else {
                BindException bindException = new BindException(errorProcessor.getTarget(),
                        errorProcessor.getObjectName());
                bindException.addError(new FieldError(errorProcessor.getObjectName(),
                        "interestPostingMonthlyFrequency",
                        errorProcessor.getRejectedValue("interestPostingMonthlyFrequency"), true,
                        new String[] { "Min.savingsProduct.interestPostingMonthlyFrequency" }, null, null));
                errors.addAllErrors(bindException);
            }
        }
    }

    BigDecimal minBalanceForInterestCalculation;
    try {
        minBalanceForInterestCalculation = BigDecimal
                .valueOf(Double.valueOf(formBean.getMinBalanceRequiredForInterestCalculation()));
    } catch (NumberFormatException e) {
        minBalanceForInterestCalculation = new BigDecimal("-1");
    }
    if (minBalanceForInterestCalculation.compareTo(BigDecimal.ZERO) < 0) {
        errors.reject("Min.savingsProduct.balanceRequiredForInterestCalculation");
    }

    if (formBean.getSelectedPrincipalGlCode().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedPrincipalGlCode");
    }

    if (formBean.getSelectedInterestGlCode().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedInterestGlCode");
    }
    Short digsAfterDec = configurationServiceFacade.getAccountingConfiguration().getDigitsAfterDecimal();
    Short digsBeforeDec = configurationServiceFacade.getAccountingConfiguration().getDigitsBeforeDecimal();
    Double maxWithdrawalObj = formBean.getMaxWithdrawalAmount();
    String maxWithdrawalString;

    if (maxWithdrawalObj == null) {
        maxWithdrawalString = "";
    } else {
        maxWithdrawalString = maxWithdrawalObj.toString();
    }

    int dot = maxWithdrawalString.lastIndexOf('.');
    int max = digsAfterDec + digsBeforeDec + dot;
    int withdrawalLength = maxWithdrawalString.length();

    if (maxWithdrawalString.lastIndexOf(0, dot) > digsBeforeDec) {
        errors.reject("MaxDigitsBefore.savingProduct.withdrawal", new String[] { digsBeforeDec.toString() },
                null);
    }
    if (maxWithdrawalString.lastIndexOf(dot, withdrawalLength) > digsAfterDec) {
        errors.reject("MaxDigitsAfter.savingProduct.withdrawal", new String[] { digsAfterDec.toString() },
                null);
    }
    if (withdrawalLength > max) {
        errors.reject("MaxDigitsNumber.savingProduct.withdrawal", new String[] { String.valueOf(max) }, null);
    }

    Double amountForDepositObj = formBean.getAmountForDeposit();
    String amountForDepositString;

    if (amountForDepositObj == null) {
        amountForDepositString = "";
    } else {
        amountForDepositString = amountForDepositObj.toString();
    }

    int depositLength = amountForDepositString.length();

    if (amountForDepositString.lastIndexOf(0, dot) > digsBeforeDec) {
        errors.reject("MaxDigitsBefore.savingProduct.deposit", new String[] { digsBeforeDec.toString() }, null);
    }
    if (amountForDepositString.lastIndexOf(dot, depositLength) > digsAfterDec) {
        errors.reject("MaxDigitsAfter.savingProduct.deposit", new String[] { digsAfterDec.toString() }, null);
    }
    if (depositLength > max) {
        errors.reject("MaxDigitsNumber.savingProduct.deposit", new String[] { String.valueOf(max) }, null);
    }

}