Example usage for org.joda.time LocalDate plusMonths

List of usage examples for org.joda.time LocalDate plusMonths

Introduction

In this page you can find the example usage for org.joda.time LocalDate plusMonths.

Prototype

public LocalDate plusMonths(int months) 

Source Link

Document

Returns a copy of this date plus the specified number of months.

Usage

From source file:com.axelor.apps.stock.service.TrackingNumberService.java

License:Open Source License

public TrackingNumber createTrackingNumber(Product product, Company company, LocalDate date)
        throws AxelorException {

    TrackingNumber trackingNumber = new TrackingNumber();

    if (product.getIsPerishable()) {
        trackingNumber.setPerishableExpirationDate(date.plusMonths(product.getPerishableNbrOfMonths()));
    }//from  ww  w. j  a  v a2 s. c om
    if (product.getHasWarranty()) {
        trackingNumber.setWarrantyExpirationDate(date.plusMonths(product.getWarrantyNbrOfMonths()));
    }

    trackingNumber.setProduct(product);
    trackingNumber.setCounter(BigDecimal.ZERO);

    TrackingNumberConfiguration trackingNumberConfiguration = product.getTrackingNumberConfiguration();

    if (trackingNumberConfiguration.getSequence() == null) {
        throw new AxelorException(String.format(I18n.get(IExceptionMessage.TRACKING_NUMBER_1),
                company.getName(), product.getCode()), IException.CONFIGURATION_ERROR);
    }

    String seq = sequenceService.getSequenceNumber(trackingNumberConfiguration.getSequence());

    trackingNumber.setTrackingNumberSeq(seq);

    return trackingNumber;
}

From source file:com.axelor.apps.tool.date.DateTool.java

License:Open Source License

private static int days360Between(LocalDate startDate, LocalDate endDate) {

    int nbDayOfFirstMonth = 0;
    int nbDayOfOthersMonths = 0;
    int nbDayOfLastMonth = 0;

    LocalDate start = startDate;//from w  w w  . j  av a 2s.c  om

    if (endDate.getMonthOfYear() != startDate.getMonthOfYear() || endDate.getYear() != startDate.getYear()) {

        // First month :: if the startDate is not the last day of the month
        if (!startDate.isEqual(startDate.dayOfMonth().withMaximumValue())) {
            nbDayOfFirstMonth = 30 - startDate.getDayOfMonth();
        }

        // The startDate is included
        nbDayOfFirstMonth = nbDayOfFirstMonth + 1;

        // Months between the first one and the last one
        LocalDate date1 = startDate.plusMonths(1).dayOfMonth().withMinimumValue();
        while (endDate.getMonthOfYear() != date1.getMonthOfYear() || endDate.getYear() != date1.getYear()) {

            nbDayOfOthersMonths = nbDayOfOthersMonths + 30;
            date1 = date1.plusMonths(1);

        }

        // Last Month
        start = endDate.dayOfMonth().withMinimumValue();
    }

    if (endDate.isEqual(endDate.dayOfMonth().withMaximumValue())) {
        nbDayOfLastMonth = 30 - start.getDayOfMonth();
    } else {
        nbDayOfLastMonth = endDate.getDayOfMonth() - start.getDayOfMonth();
    }

    // The endDate is included
    nbDayOfLastMonth = nbDayOfLastMonth + 1;

    return nbDayOfFirstMonth + nbDayOfOthersMonths + nbDayOfLastMonth;
}

From source file:com.creditcloud.wealthproduct.model.utils.WealthProductCalculator.java

/**
 * ???.//from  w  w w.j a  va  2 s.  c o  m
 *
 * @param amount
 * @param duration
 * @param rate             2400 means 24.00%
 * @param method
 * @param asOfDate         ?131?: 228(29)?331?430
 * @param period           
 * @param repayDateOfMonth 
 * @return
 */
public static WealthProductRepaymentDetail analyze(final BigDecimal amount, final Duration duration,
        final int rate, final RepaymentMethod method, final LocalDate asOfDate, final RepaymentPeriod period,
        final int repayDateOfMonth) {
    if (period == null) {
        return analyze(amount, duration, rate, method, asOfDate);
    }
    if (repayDateOfMonth == 0) {
        return analyze(amount, duration, rate, method, asOfDate, period);
    }
    if (!method.isExtensible()) {
        throw new IllegalArgumentException(method + "is not extensible repayment.");
    }
    if (!method.equals(BulletRepayment)) {//???
        //periodRepaymentMethod???
        ValidateResult validateResult = validatePeriodAndDuration(period, duration);
        if (!validateResult.isSuccess()) {
            throw new IllegalArgumentException(validateResult.getMessage());
        }
    }
    WealthProductRepaymentDetail result = null;

    //principal
    BigDecimal principal = amount;
    //now get rates
    BigDecimal rateYear = new BigDecimal(rate).divide(rateScale, mc);
    BigDecimal rateMonth = rateYear.divide(monthsPerYear, mc);
    BigDecimal ratePeriod = rateMonth.multiply(new BigDecimal(period.getMonthsOfPeriod()));

    //??
    int fixDays = repayDateOfMonth - asOfDate.getDayOfMonth();
    int fixMonth = asOfDate.getMonthOfYear() - 1;
    //31??31?
    LocalDate fixAsOfDate = new LocalDate(asOfDate.getYear(), 1, repayDateOfMonth);
    if (fixDays < 0) {
        fixAsOfDate = fixAsOfDate.plusMonths(1);
    }

    //dealing with different methods
    BigDecimal interest, amortizedInterest, amortizedPrincipal, outstandingPrincipal;

    int tenure;
    switch (method) {
    case BulletRepayment:
        //???
        analyze(amount, duration, rate, method, asOfDate);
        break;
    case MonthlyInterest: //period?
        tenure = duration.getTotalMonths() / period.getMonthsOfPeriod();
        amortizedInterest = principal.multiply(ratePeriod).setScale(2, NumberConstant.ROUNDING_MODE);
        interest = amortizedInterest.multiply(new BigDecimal(tenure));
        //create result
        result = new WealthProductRepaymentDetail(principal, interest, duration, method,
                new ArrayList<Repayment>());

        //add amortized items
        for (int i = 0; i < tenure; i++) {
            if (i < tenure - 1) { //only interest, no principal
                result.getRepayments()
                        .add(new Repayment(ZERO, amortizedInterest, principal, DateUtils.offset(fixAsOfDate,
                                new Duration(0, (i + 1) * period.getMonthsOfPeriod() + fixMonth, 0))));
            } else { //last ONE we pay off the principal as well as interest
                result.getRepayments().add(new Repayment(principal, amortizedInterest, ZERO,
                        DateUtils.offset(asOfDate, new Duration(0, (i + 1) * period.getMonthsOfPeriod(), 0))));
            }
        }
        break;
    case EqualInstallment: //period??
        //times of repayments in months
        tenure = (duration.getYears() * 12 + duration.getMonths()) / period.getMonthsOfPeriod();
        BigDecimal[] is = new BigDecimal[tenure + 1];
        for (int i = 0; i <= tenure; i++) {
            is[i] = ratePeriod.add(ONE).pow(i);
        }
        BigDecimal baseInterest = principal.multiply(ratePeriod);
        //calc installment
        BigDecimal installment = baseInterest.multiply(is[tenure]).divide(is[tenure].subtract(ONE), mc);
        installment = installment.setScale(2, NumberConstant.ROUNDING_MODE);
        //reset total interest
        interest = ZERO;
        //create WealthProductRepaymentDetail
        result = new WealthProductRepaymentDetail(principal, interest, duration, method,
                new ArrayList<Repayment>());
        //deal with amortized items
        outstandingPrincipal = principal;
        for (int i = 0; i < tenure; i++) {
            amortizedInterest = baseInterest.subtract(installment, mc).multiply(is[i]).add(installment, mc)
                    .setScale(2, NumberConstant.ROUNDING_MODE);
            amortizedPrincipal = installment.subtract(amortizedInterest);
            outstandingPrincipal = outstandingPrincipal.subtract(amortizedPrincipal);
            if (i == tenure - 1) { //last ONE we need to fix the rounding error and let the oustanding principal be ZERO
                result.getRepayments().add(new Repayment(amortizedPrincipal.add(outstandingPrincipal),
                        amortizedInterest, ZERO,
                        DateUtils.offset(asOfDate, new Duration(0, (i + 1) * period.getMonthsOfPeriod(), 0))));
            } else {
                result.getRepayments()
                        .add(new Repayment(amortizedPrincipal, amortizedInterest, outstandingPrincipal,
                                DateUtils.offset(fixAsOfDate,
                                        new Duration(0, (i + 1) * period.getMonthsOfPeriod() + fixMonth, 0))));
            }
            interest = interest.add(amortizedInterest);
        }
        //fix interest
        result.setInterest(interest);
        break;
    case EqualPrincipal: //period?
        //times of repayments in months
        tenure = (duration.getYears() * 12 + duration.getMonths()) / period.getMonthsOfPeriod();
        //calc amortized principal first
        amortizedPrincipal = principal.divide(new BigDecimal(tenure), mc).setScale(2,
                NumberConstant.ROUNDING_MODE);
        //calc by each month
        BigDecimal[] interests = new BigDecimal[tenure];
        BigDecimal[] outstandingPrincipals = new BigDecimal[tenure];
        outstandingPrincipal = principal;
        interest = ZERO;
        for (int i = 0; i < tenure; i++) {
            interests[i] = outstandingPrincipal.multiply(ratePeriod, mc).setScale(2,
                    NumberConstant.ROUNDING_MODE);
            interest = interest.add(interests[i]);
            outstandingPrincipal = outstandingPrincipal.subtract(amortizedPrincipal);
            outstandingPrincipals[i] = outstandingPrincipal;
        }
        //create WealthProductRepaymentDetail
        result = new WealthProductRepaymentDetail(principal, interest, duration, method,
                new ArrayList<Repayment>());
        //deal with amortized items
        for (int i = 0; i < tenure; i++) {
            if (i == tenure - 1) {
                result.getRepayments().add(new Repayment(amortizedPrincipal.add(outstandingPrincipals[i]),
                        interests[i], ZERO,
                        DateUtils.offset(asOfDate, new Duration(0, (i + 1) * period.getMonthsOfPeriod(), 0))));
            } else {
                result.getRepayments()
                        .add(new Repayment(amortizedPrincipal, interests[i], outstandingPrincipals[i],
                                DateUtils.offset(fixAsOfDate,
                                        new Duration(0, (i + 1) * period.getMonthsOfPeriod() + fixMonth, 0))));
            }
        }
        break;
    case EqualInterest: //period?
        //times of repayments in months
        tenure = (duration.getYears() * 12 + duration.getMonths()) / period.getMonthsOfPeriod();
        //calc amortized principal and interest
        amortizedPrincipal = principal.divide(new BigDecimal(tenure), mc).setScale(2,
                NumberConstant.ROUNDING_MODE);
        amortizedInterest = principal.multiply(ratePeriod).setScale(2, NumberConstant.ROUNDING_MODE);
        interest = amortizedInterest.multiply(new BigDecimal(tenure), mc).setScale(2,
                NumberConstant.ROUNDING_MODE);
        //create WealthProductRepaymentDetail
        result = new WealthProductRepaymentDetail(principal, interest, duration, method,
                new ArrayList<Repayment>());
        //deal with amortized items
        outstandingPrincipal = principal;
        for (int i = 0; i < tenure; i++) {
            outstandingPrincipal = outstandingPrincipal.subtract(amortizedPrincipal);
            if (i == tenure - 1) {
                result.getRepayments().add(new Repayment(amortizedPrincipal.add(outstandingPrincipal),
                        amortizedInterest, ZERO,
                        DateUtils.offset(asOfDate, new Duration(0, (i + 1) * period.getMonthsOfPeriod(), 0))));
            } else {
                result.getRepayments()
                        .add(new Repayment(amortizedPrincipal, amortizedInterest, outstandingPrincipal,
                                DateUtils.offset(fixAsOfDate,
                                        new Duration(0, (i + 1) * period.getMonthsOfPeriod() + fixMonth, 0))));
            }
        }
        break;
    }

    return result;
}

From source file:com.gst.integrationtests.common.shares.ShareProductHelper.java

License:Apache License

public ShareProductHelper withMarketPrice() {
    this.marketPrices = new ArrayList<>();
    LocalDate currentDate = DateUtils.getLocalDateOfTenant();
    String[] prices = { "3.0", "4.0", "5.0", "6.0", "7.0" };
    DateFormat simple = new SimpleDateFormat("dd MMMM yyyy");
    for (int i = 0; i < prices.length; i++) {
        currentDate = currentDate.plusMonths(2);
        Map<String, String> marketPrice = new HashMap<>();
        marketPrice.put("fromDate", simple.format(currentDate));
        marketPrice.put("shareValue", prices[i]);
        this.marketPrices.add(marketPrice);
    }/* www .  j a va  2  s. c o m*/
    return this;
}

From source file:com.gst.portfolio.account.service.StandingInstructionWritePlatformServiceImpl.java

License:Apache License

@Override
@CronTarget(jobName = JobName.EXECUTE_STANDING_INSTRUCTIONS)
public void executeStandingInstructions() throws JobExecutionException {
    Collection<StandingInstructionData> instructionDatas = this.standingInstructionReadPlatformService
            .retrieveAll(StandingInstructionStatus.ACTIVE.getValue());
    final StringBuilder sb = new StringBuilder();
    for (StandingInstructionData data : instructionDatas) {
        boolean isDueForTransfer = false;
        AccountTransferRecurrenceType recurrenceType = data.recurrenceType();
        StandingInstructionType instructionType = data.instructionType();
        LocalDate transactionDate = new LocalDate();
        if (recurrenceType.isPeriodicRecurrence()) {
            final ScheduledDateGenerator scheduledDateGenerator = new DefaultScheduledDateGenerator();
            PeriodFrequencyType frequencyType = data.recurrenceFrequency();
            LocalDate startDate = data.validFrom();
            if (frequencyType.isMonthly()) {
                startDate = startDate.withDayOfMonth(data.recurrenceOnDay());
                if (startDate.isBefore(data.validFrom())) {
                    startDate = startDate.plusMonths(1);
                }//from   w w  w.j  a v a  2s.c  o  m
            } else if (frequencyType.isYearly()) {
                startDate = startDate.withDayOfMonth(data.recurrenceOnDay())
                        .withMonthOfYear(data.recurrenceOnMonth());
                if (startDate.isBefore(data.validFrom())) {
                    startDate = startDate.plusYears(1);
                }
            }
            isDueForTransfer = scheduledDateGenerator.isDateFallsInSchedule(frequencyType,
                    data.recurrenceInterval(), startDate, transactionDate);

        }
        BigDecimal transactionAmount = data.amount();
        if (data.toAccountType().isLoanAccount() && (recurrenceType.isDuesRecurrence()
                || (isDueForTransfer && instructionType.isDuesAmoutTransfer()))) {
            StandingInstructionDuesData standingInstructionDuesData = this.standingInstructionReadPlatformService
                    .retriveLoanDuesData(data.toAccount().accountId());
            if (data.instructionType().isDuesAmoutTransfer()) {
                transactionAmount = standingInstructionDuesData.totalDueAmount();
            }
            if (recurrenceType.isDuesRecurrence()) {
                isDueForTransfer = new LocalDate().equals(standingInstructionDuesData.dueDate());
            }
        }

        if (isDueForTransfer && transactionAmount != null && transactionAmount.compareTo(BigDecimal.ZERO) > 0) {
            final SavingsAccount fromSavingsAccount = null;
            final boolean isRegularTransaction = true;
            final boolean isExceptionForBalanceCheck = false;
            AccountTransferDTO accountTransferDTO = new AccountTransferDTO(transactionDate, transactionAmount,
                    data.fromAccountType(), data.toAccountType(), data.fromAccount().accountId(),
                    data.toAccount().accountId(), data.name() + " Standing instruction trasfer ", null, null,
                    null, null, data.toTransferType(), null, null, data.transferType().getValue(), null, null,
                    null, null, null, fromSavingsAccount, isRegularTransaction, isExceptionForBalanceCheck);
            final boolean transferCompleted = transferAmount(sb, accountTransferDTO, data.getId());

            if (transferCompleted) {
                final String updateQuery = "UPDATE m_account_transfer_standing_instructions SET last_run_date = ? where id = ?";
                this.jdbcTemplate.update(updateQuery, transactionDate.toDate(), data.getId());
            }

        }
    }
    if (sb.length() > 0) {
        throw new JobExecutionException(sb.toString());
    }

}

From source file:com.gst.portfolio.calendar.service.CalendarUtils.java

License:Apache License

public static LocalDate getRecentEligibleMeetingDate(final String recurringRule, final LocalDate seedDate,
        final boolean isSkipMeetingOnFirstDay, final Integer numberOfDays) {
    LocalDate currentDate = DateUtils.getLocalDateOfTenant();
    final Recur recur = CalendarUtils.getICalRecur(recurringRule);
    if (recur == null) {
        return null;
    }/*w w w  .j a  va2s.co m*/

    if (isValidRecurringDate(recur, seedDate, currentDate, isSkipMeetingOnFirstDay, numberOfDays)) {
        return currentDate;
    }

    if (recur.getFrequency().equals(Recur.DAILY)) {
        currentDate = currentDate.plusDays(recur.getInterval());
    } else if (recur.getFrequency().equals(Recur.WEEKLY)) {
        currentDate = currentDate.plusWeeks(recur.getInterval());
    } else if (recur.getFrequency().equals(Recur.MONTHLY)) {
        currentDate = currentDate.plusMonths(recur.getInterval());
    } else if (recur.getFrequency().equals(Recur.YEARLY)) {
        currentDate = currentDate.plusYears(recur.getInterval());
    }

    return getNextRecurringDate(recur, seedDate, currentDate);
}

From source file:com.gst.portfolio.loanaccount.domain.Loan.java

License:Apache License

private LocalDate getMaxDateLimitForNewRepayment(final PeriodFrequencyType periodFrequencyType,
        final Integer loanRepaymentInterval, final LocalDate startDate) {
    LocalDate dueRepaymentPeriodDate = startDate;
    final Integer repaidEvery = 2 * loanRepaymentInterval;
    switch (periodFrequencyType) {
    case DAYS:/*from w w  w . j  a  v  a 2 s. c  om*/
        dueRepaymentPeriodDate = startDate.plusDays(repaidEvery);
        break;
    case WEEKS:
        dueRepaymentPeriodDate = startDate.plusWeeks(repaidEvery);
        break;
    case MONTHS:
        dueRepaymentPeriodDate = startDate.plusMonths(repaidEvery);
        break;
    case YEARS:
        dueRepaymentPeriodDate = startDate.plusYears(repaidEvery);
        break;
    case INVALID:
        break;
    }
    return dueRepaymentPeriodDate.minusDays(1);// get 2n-1 range date from
                                               // startDate
}

From source file:com.gst.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator.java

License:Apache License

@Override
public LocalDate getRepaymentPeriodDate(final PeriodFrequencyType frequency, final int repaidEvery,
        final LocalDate startDate, Integer nthDay, DayOfWeekType dayOfWeek) {
    LocalDate dueRepaymentPeriodDate = startDate;
    switch (frequency) {
    case DAYS:/*w w  w .  j a va 2  s  . co m*/
        dueRepaymentPeriodDate = startDate.plusDays(repaidEvery);
        break;
    case WEEKS:
        dueRepaymentPeriodDate = startDate.plusWeeks(repaidEvery);
        break;
    case MONTHS:
        dueRepaymentPeriodDate = startDate.plusMonths(repaidEvery);
        if (!(nthDay == null || dayOfWeek == null || dayOfWeek == DayOfWeekType.INVALID)) {
            dueRepaymentPeriodDate = adjustToNthWeekDay(dueRepaymentPeriodDate, nthDay, dayOfWeek.getValue());
        }
        break;
    case YEARS:
        dueRepaymentPeriodDate = startDate.plusYears(repaidEvery);
        break;
    case INVALID:
        break;
    }
    return dueRepaymentPeriodDate;
}

From source file:com.gst.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator.java

License:Apache License

@Override
public Boolean isDateFallsInSchedule(final PeriodFrequencyType frequency, final int repaidEvery,
        final LocalDate startDate, final LocalDate date) {
    boolean isScheduledDate = false;
    switch (frequency) {
    case DAYS:/*w w w  . java 2 s.  c o m*/
        int diff = Days.daysBetween(startDate, date).getDays();
        isScheduledDate = (diff % repaidEvery) == 0;
        break;
    case WEEKS:
        int weekDiff = Weeks.weeksBetween(startDate, date).getWeeks();
        isScheduledDate = (weekDiff % repaidEvery) == 0;
        if (isScheduledDate) {
            LocalDate modifiedDate = startDate.plusWeeks(weekDiff);
            isScheduledDate = modifiedDate.isEqual(date);
        }
        break;
    case MONTHS:
        int monthDiff = Months.monthsBetween(startDate, date).getMonths();
        isScheduledDate = (monthDiff % repaidEvery) == 0;
        if (isScheduledDate) {
            LocalDate modifiedDate = startDate.plusMonths(monthDiff);
            isScheduledDate = modifiedDate.isEqual(date);
        }
        break;
    case YEARS:
        int yearDiff = Years.yearsBetween(startDate, date).getYears();
        isScheduledDate = (yearDiff % repaidEvery) == 0;
        if (isScheduledDate) {
            LocalDate modifiedDate = startDate.plusYears(yearDiff);
            isScheduledDate = modifiedDate.isEqual(date);
        }
        break;
    case INVALID:
        break;
    }
    return isScheduledDate;
}

From source file:com.gst.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java

License:Apache License

private LocalDate getPeriodEndDate(final LocalDate startDate) {
    LocalDate dueRepaymentPeriodDate = startDate;
    switch (this.repaymentPeriodFrequencyType) {
    case DAYS:/*ww w.  ja v  a  2s  .  c om*/
        dueRepaymentPeriodDate = startDate.plusDays(this.repaymentEvery);
        break;
    case WEEKS:
        dueRepaymentPeriodDate = startDate.plusWeeks(this.repaymentEvery);
        break;
    case MONTHS:
        dueRepaymentPeriodDate = startDate.plusMonths(this.repaymentEvery);
        break;
    case YEARS:
        dueRepaymentPeriodDate = startDate.plusYears(this.repaymentEvery);
        break;
    case INVALID:
        break;
    }
    return dueRepaymentPeriodDate;
}