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:gg.db.datamodel.Periods.java

License:Open Source License

/**
 * Gets the list of Period between a start date and an end date<BR/><BR/>
 * Example:<BR/>/*from   www. j a  v a  2s.  c om*/
 * getListOfPeriod(new LocalDate(2006, 5, 20), new LocalDate(2006, 7, 2), PeriodType.MONTH) should create the following Period objects:
 * <UL>
 * <LI> From 05/01/2006 to 05/31/2006 (MONTH)</LI>
 * <LI> From 06/01/2006 to 06/30/2006 (MONTH)</LI>
 * <LI> From 07/01/2006 to 07/31/2006 (MONTH)</LI>
 * </UL>
 * @param start Start date of the whole period
 * @param end End date of the whole period
 * @param periodType Type of the Period to create
 * @return The list of computed Period
 */
private List<Period> getListOfPeriod(LocalDate start, LocalDate end, PeriodType periodType) {
    if (start == null || end == null || periodType == null) {
        throw new IllegalArgumentException(
                "One of the following parameters is null: 'start', 'end', 'periodType'");
    }

    // Adjust the start date to the start of the period's type (first day of the week, first day of the month, first day of the year depending on the type of the period to create)
    LocalDate startPeriods = getAdjustedStartDate(start, periodType);

    // Adjust the end date to the end of the period's type (last day of the week, last day of the month, last day of the year depending on the type of the period to create)
    LocalDate endPeriods = getAdjustedEndDate(end, periodType);

    assert (startPeriods != null && endPeriods != null);

    // Create the list of Period objects
    List<Period> listOfPeriod = new ArrayList<Period>(); // List of created Period (returned object)
    Period newPeriod;
    if (periodType == PeriodType.FREE) {
        // Create the Period object and add it to the list of Period
        newPeriod = new Period(start, end, PeriodType.FREE);
        listOfPeriod.add(newPeriod);
    } else {
        // Create all Period objects and add them to the list of Period
        while (startPeriods.compareTo(endPeriods) <= 0) {
            switch (periodType) {
            case DAY:
                // Create the DAY period
                newPeriod = new Period(startPeriods, startPeriods, PeriodType.DAY); // i.e. Day from 05/02/2006 to 05/02/2006
                startPeriods = startPeriods.plusDays(1); // Move to the next day
                break;
            case WEEK:
                // Create the WEEK period
                newPeriod = new Period(startPeriods, startPeriods.plusDays(6), PeriodType.WEEK); // i.e. Week from 05/01/2006 (MONDAY) to 05/07/2006 (SUNDAY)
                startPeriods = startPeriods.plusDays(7); // Move to the next week
                break;
            case MONTH:
                // Create the MONTH period
                newPeriod = new Period(startPeriods, startPeriods.plusMonths(1).minusDays(1), PeriodType.MONTH); // i.e. Month from 05/01/2006 to 05/31/2006
                startPeriods = startPeriods.plusMonths(1); // Move to the next month
                break;
            case YEAR:
                // Create the YEAR period
                newPeriod = new Period(startPeriods, startPeriods.plusYears(1).minusDays(1), PeriodType.YEAR); // i.e. Year from 01/01/2006 to 12/31/2006
                startPeriods = startPeriods.plusYears(1); // Move to the next year
                break;
            case FREE: // should never happen
                throw new AssertionError("The FREE PeriodType should not be handled in this switch statement");
            default: // should never happen
                throw new AssertionError("Unknown PeriodType");
            }
            assert (newPeriod != null);

            // Add the created period to the list of Period
            listOfPeriod.add(newPeriod);
        }
    }

    return listOfPeriod;
}

From source file:net.jrkatz.minero.data.MonthlyPeriodDefinition.java

License:Open Source License

private Period periodForMonth(final int year, final int month) {
    LocalDate start = startForMonth(year, month);
    LocalDate nextMonth = start.plusMonths(1);
    LocalDate end = startForMonth(nextMonth.getYear(), nextMonth.getMonthOfYear());
    return new Period(start, end);
}

From source file:net.objectlab.kit.datecalc.joda.LocalDateIMMDateCalculator.java

License:Apache License

private LocalDate calculateIMMMonth(final boolean requestNextIMM, final LocalDate startDate, final int month) {
    int monthOffset = 0;
    LocalDate date = startDate;
    switch (month) {
    case MARCH:// w w  w .  j  a  va2  s.com
    case JUNE:
    case SEPTEMBER:
    case DECEMBER:
        final LocalDate immDate = calculate3rdWednesday(date);
        if (requestNextIMM && !date.isBefore(immDate)) {
            date = date.plusMonths(MONTHS_IN_QUARTER);
        } else if (!requestNextIMM && !date.isAfter(immDate)) {
            date = date.minusMonths(MONTHS_IN_QUARTER);
        }
        break;

    default:
        if (requestNextIMM) {
            monthOffset = (MONTH_IN_YEAR - month) % MONTHS_IN_QUARTER;
            date = date.plusMonths(monthOffset);
        } else {
            monthOffset = month % MONTHS_IN_QUARTER;
            date = date.minusMonths(monthOffset);
        }
        break;
    }
    return date;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.academicAdministration.accounting.reports.ExportSibsPaymentsReport.java

License:Open Source License

private List<SibsPaymentFileProcessReport> getSibsPaymentFileProcessReports(final SibsPaymentsReportBean bean) {
    final LocalDate startDate = new LocalDate(bean.getYearAsInt(), bean.getMonthAsInt(), 1);
    final LocalDate endDate = startDate.plusMonths(1).minusDays(1);
    final List<SibsPaymentFileProcessReport> files = SibsPaymentFileProcessReport.readAllBetween(startDate,
            endDate);/*ww  w . j  a v  a  2 s  .com*/
    Collections.sort(files, SibsPaymentFileProcessReport.COMPARATOR_BY_SIBS_PROCESS_DATE);
    return files;
}

From source file:org.activiti.dmn.engine.impl.mvel.extension.DateUtil.java

License:Apache License

public static Date addDate(Date startDate, Integer years, Integer months, Integer days) {

    LocalDate currentDate = new LocalDate(startDate);

    currentDate = currentDate.plusYears(years);
    currentDate = currentDate.plusMonths(months);
    currentDate = currentDate.plusDays(days);

    return currentDate.toDate();
}

From source file:org.alexlg.bankit.services.SyncService.java

License:Open Source License

/**
 * This function take all the costs between
 * the last execution to create the associated
 * operations in the operation list.//from  ww w  .  jav a  2 s  .co  m
 * It runs every day at midnight and 5 seconds
 */
@Transactional
@Scheduled(cron = "5 0 0 * * *")
public void materializeCostsIntoOperation() {
    logger.info("Starting materializeCostsIntoOperation");

    //materialize operations 2 days beyond current date
    LocalDate endDate = getEndSyncDate().plusDays(2);

    Date startSync = optionsService.getDate(COST_SYNC_OPT);
    if (startSync == null) {
        optionsService.set(COST_SYNC_OPT, endDate.toDate());
        return;
    }
    LocalDate startDate = new LocalDate(startSync);

    //retrieve costs list
    List<Cost> costs = null;
    int nbMonth = 1;

    if (endDate.getYear() == startDate.getYear() && endDate.getMonthOfYear() == startDate.getMonthOfYear()) {
        //only retrieve the costs between this 2 "days"
        costs = costDao.getList(startDate.getDayOfMonth(), endDate.getDayOfMonth());
    } else {
        //getting all costs
        costs = costDao.getList();

        //we generate a least for the current month (as nbMonth = 1)
        //then we add whole months between start and end.
        nbMonth += Months.monthsBetween(startDate, endDate).getMonths();

        //as monthsBetween calculate for whole month, if start is for example the 24-09
        //and the end is the 02-10, monthsBetween will return 0 but we need to have
        //2 loops, one for september and one for november so we add a month.
        if (endDate.getDayOfMonth() <= startDate.getDayOfMonth()) {
            nbMonth++;
        }
    }

    //going through each month and each cost to create the operation
    for (int m = 0; m < nbMonth; m++) {
        LocalDate curMonth = startDate.plusMonths(m);
        int lastDayOfMonth = curMonth.dayOfMonth().getMaximumValue();

        for (Cost cost : costs) {
            int costDay = cost.getDay();

            //if the operation is planned after the last day of month
            //set it to the last day
            if (costDay > lastDayOfMonth)
                costDay = lastDayOfMonth;

            //creating operation date
            LocalDate opDate = new LocalDate(curMonth.getYear(), curMonth.getMonthOfYear(), costDay);

            //check if date is in the date interval before creating op
            if (opDate.isAfter(startDate) && opDate.compareTo(endDate) <= 0) {
                Operation op = new Operation();
                op.setOperationDate(opDate.toDate());
                op.setLabel(cost.getLabel());
                op.setPlanned(cost.getAmount());
                op.setCategory(cost.getCategory());
                operationDao.save(op);
            }
        }
    }

    optionsService.set(COST_SYNC_OPT, endDate.toDate());

}

From source file:org.apache.fineract.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  ww  . jav  a2s . 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 AccountTransferDetails accountTransferDetails = this.accountTransferDetailRepository
                    .findOne(data.accountDetailId());
            final SavingsAccount fromSavingsAccount = null;
            final boolean isRegularTransaction = true;
            final boolean isExceptionForBalanceCheck = false;
            accountTransferDetails.accountTransferStandingInstruction()
                    .updateLatsRunDate(transactionDate.toDate());
            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(),
                    accountTransferDetails, null, null, null, null, fromSavingsAccount, isRegularTransaction,
                    isExceptionForBalanceCheck);
            transferAmount(sb, accountTransferDTO, data.getId());
        }
    }
    if (sb.length() > 0) {
        throw new JobExecutionException(sb.toString());
    }

}

From source file:org.apache.fineract.portfolio.calendar.service.CalendarUtils.java

License:Apache License

public static LocalDate getRecentEligibleMeetingDate(final String recurringRule, final LocalDate seedDate) {
    LocalDate currentDate = DateUtils.getLocalDateOfTenant();
    final Recur recur = CalendarUtils.getICalRecur(recurringRule);
    if (recur == null) {
        return null;
    }/*from w w w .j  a  v a  2 s . co m*/

    if (isValidRecurringDate(recur, seedDate, currentDate)) {
        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:org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java

License:Apache License

public BigDecimal calculatePeriodsBetweenDates(final LocalDate startDate, final LocalDate endDate) {
    BigDecimal numberOfPeriods = BigDecimal.ZERO;
    switch (this.repaymentPeriodFrequencyType) {
    case DAYS:/*w w  w.  j  a  v  a2  s.c o m*/
        int numberOfDays = Days.daysBetween(startDate, endDate).getDays();
        numberOfPeriods = BigDecimal.valueOf((double) numberOfDays);
        break;
    case WEEKS:
        int numberOfWeeks = Weeks.weeksBetween(startDate, endDate).getWeeks();
        int daysLeftAfterWeeks = Days.daysBetween(startDate.plusWeeks(numberOfWeeks), endDate).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfWeeks))
                .add(BigDecimal.valueOf((double) daysLeftAfterWeeks / 7));
        break;
    case MONTHS:
        int numberOfMonths = Months.monthsBetween(startDate, endDate).getMonths();
        LocalDate startDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths);
        LocalDate endDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths + 1);
        int daysLeftAfterMonths = Days.daysBetween(startDateAfterConsideringMonths, endDate).getDays();
        int daysInPeriodAfterMonths = Days
                .daysBetween(startDateAfterConsideringMonths, endDateAfterConsideringMonths).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfMonths))
                .add(BigDecimal.valueOf((double) daysLeftAfterMonths / daysInPeriodAfterMonths));
        break;
    case YEARS:
        int numberOfYears = Years.yearsBetween(startDate, endDate).getYears();
        LocalDate startDateAfterConsideringYears = startDate.plusYears(numberOfYears);
        LocalDate endDateAfterConsideringYears = startDate.plusYears(numberOfYears + 1);
        int daysLeftAfterYears = Days.daysBetween(startDateAfterConsideringYears, endDate).getDays();
        int daysInPeriodAfterYears = Days
                .daysBetween(startDateAfterConsideringYears, endDateAfterConsideringYears).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfYears))
                .add(BigDecimal.valueOf((double) daysLeftAfterYears / daysInPeriodAfterYears));
        break;
    default:
        break;
    }
    return numberOfPeriods;
}

From source file:org.apache.fineract.portfolio.loanaccount.loanschedule.service.LoanScheduleAssembler.java

License:Apache License

private LocalDate deriveFirstRepaymentDate(final AccountType loanType, final Integer repaymentEvery,
        final LocalDate expectedDisbursementDate, final PeriodFrequencyType repaymentPeriodFrequencyType,
        final Integer minimumDaysBetweenDisbursalAndFirstRepayment, final Calendar calendar) {

    LocalDate derivedFirstRepayment = null;

    final LocalDate dateBasedOnMinimumDaysBetweenDisbursalAndFirstRepayment = expectedDisbursementDate
            .plusDays(minimumDaysBetweenDisbursalAndFirstRepayment);

    if ((loanType.isJLGAccount() || loanType.isGroupAccount()) && calendar != null) {

        final LocalDate refernceDateForCalculatingFirstRepaymentDate = expectedDisbursementDate;
        derivedFirstRepayment = deriveFirstRepaymentDateForJLGLoans(repaymentEvery, expectedDisbursementDate,
                refernceDateForCalculatingFirstRepaymentDate, repaymentPeriodFrequencyType,
                minimumDaysBetweenDisbursalAndFirstRepayment, calendar);

    } /*** Individual or group account, or JLG not linked to a meeting ***/
    else {/*w w w  . j  a va2 s .co  m*/
        LocalDate dateBasedOnRepaymentFrequency;
        // Derive the first repayment date as greater date among
        // (disbursement date + plus frequency) or
        // (disbursement date + minimum between disbursal and first
        // repayment )
        if (repaymentPeriodFrequencyType.isDaily()) {
            dateBasedOnRepaymentFrequency = expectedDisbursementDate.plusDays(repaymentEvery);
        } else if (repaymentPeriodFrequencyType.isWeekly()) {
            dateBasedOnRepaymentFrequency = expectedDisbursementDate.plusWeeks(repaymentEvery);
        } else if (repaymentPeriodFrequencyType.isMonthly()) {
            dateBasedOnRepaymentFrequency = expectedDisbursementDate.plusMonths(repaymentEvery);
        } /** yearly loan **/
        else {
            dateBasedOnRepaymentFrequency = expectedDisbursementDate.plusYears(repaymentEvery);
        }
        derivedFirstRepayment = dateBasedOnRepaymentFrequency.isAfter(
                dateBasedOnMinimumDaysBetweenDisbursalAndFirstRepayment) ? dateBasedOnRepaymentFrequency
                        : dateBasedOnMinimumDaysBetweenDisbursalAndFirstRepayment;
    }

    return derivedFirstRepayment;
}