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.gst.portfolio.shareaccounts.serialization.ShareAccountDataSerializer.java

License:Apache License

private LocalDate deriveLockinPeriodDuration(final Integer lockinPeriod, final PeriodFrequencyType periodType,
        LocalDate purchaseDate) {
    LocalDate lockinDate = purchaseDate;
    if (periodType != null) {
        switch (periodType) {
        case INVALID: //It never comes in to this state.
            break;
        case DAYS:
            lockinDate = purchaseDate.plusDays(lockinPeriod);
            break;
        case WEEKS:
            lockinDate = purchaseDate.plusWeeks(lockinPeriod);
            break;
        case MONTHS:
            lockinDate = purchaseDate.plusMonths(lockinPeriod);
            break;
        case YEARS:
            lockinDate = purchaseDate.plusYears(lockinPeriod);
            break;
        }/*  w ww .  j  a v a  2 s  .  c  om*/
    }
    return lockinDate;
}

From source file:com.mars.test.jodatime.Mars_App.java

public static void main(String[] args) {
    // LocalDate  , TimeZone
    LocalDate dt = new LocalDate();

    // 1.?1~31//from  ww w.  ja  v  a2 s  . co  m
    log.info("1.?" + dt.withDayOfMonth(1).toString(pattern) + " ~ "
            + dt.dayOfMonth().withMaximumValue().toString(pattern));

    // 2.?26~25(????)
    log.info("2.?" + dt.minusMonths(1).withDayOfMonth(26).toString(pattern) + " ~ "
            + dt.withDayOfMonth(25).toString(pattern));

    // 3.???
    LocalDate date2 = dt.withDayOfMonth(5);
    if (date2.getDayOfWeek() == DateTimeConstants.SATURDAY
            || date2.getDayOfWeek() == DateTimeConstants.SUNDAY) {
        date2 = date2.plusWeeks(1).withDayOfWeek(1);
    }
    log.info("3." + date2.toString(pattern));

    LocalDate date3 = dt.plusMonths(1).withDayOfMonth(5);
    if (date3.getDayOfWeek() >= 6) {
        date3 = date3.plusWeeks(1).withDayOfWeek(1);
    }
    log.info("4.2014/7" + date3.toString(pattern));

}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static BigDecimal calculateProRationBeforeFirstBillingPeriod(final LocalDate startDate,
        final LocalDate nextBillingCycleDate, final BillingPeriod billingPeriod) {
    final LocalDate previousBillingCycleDate = nextBillingCycleDate
            .plusMonths(-billingPeriod.getNumberOfMonths());

    return calculateProrationBetweenDates(startDate, nextBillingCycleDate, previousBillingCycleDate,
            nextBillingCycleDate);//from www .ja v  a  2  s . co  m
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateLastBillingCycleDateBefore(final LocalDate date,
        final LocalDate previousBillCycleDate, final int billingCycleDay, final BillingPeriod billingPeriod) {
    LocalDate proposedDate = previousBillCycleDate;

    int numberOfPeriods = 0;
    while (!proposedDate.isAfter(date)) {
        proposedDate = previousBillCycleDate.plusMonths(numberOfPeriods * billingPeriod.getNumberOfMonths());
        numberOfPeriods += 1;//  w  w  w .  j a va  2 s.  c o  m
    }

    proposedDate = proposedDate.plusMonths(-billingPeriod.getNumberOfMonths());

    if (proposedDate.dayOfMonth().get() < billingCycleDay) {
        final int lastDayOfTheMonth = proposedDate.dayOfMonth().getMaximumValue();
        if (lastDayOfTheMonth < billingCycleDay) {
            proposedDate = new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(),
                    lastDayOfTheMonth);
        } else {
            proposedDate = new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(),
                    billingCycleDay);
        }
    }

    if (proposedDate.isBefore(previousBillCycleDate)) {
        // Make sure not to go too far in the past
        return previousBillCycleDate;
    } else {
        return proposedDate;
    }
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateEffectiveEndDate(final LocalDate billCycleDate, final LocalDate targetDate,
        final BillingPeriod billingPeriod) {
    if (targetDate.isBefore(billCycleDate)) {
        return billCycleDate;
    }// w w  w.  j av  a 2s. c  o m

    final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths();
    int numberOfPeriods = 0;
    LocalDate proposedDate = billCycleDate;

    while (!proposedDate.isAfter(targetDate)) {
        proposedDate = billCycleDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
        numberOfPeriods += 1;
    }

    return proposedDate;
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateEffectiveEndDate(final LocalDate billCycleDate, final LocalDate targetDate,
        final LocalDate endDate, final BillingPeriod billingPeriod) {
    if (targetDate.isBefore(endDate)) {
        if (targetDate.isBefore(billCycleDate)) {
            return billCycleDate;
        }// w w w .  j  a v  a2 s . c o m

        final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths();
        int numberOfPeriods = 0;
        LocalDate proposedDate = billCycleDate;

        while (!proposedDate.isAfter(targetDate)) {
            proposedDate = billCycleDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
            numberOfPeriods += 1;
        }

        // the current period includes the target date
        // check to see whether the end date truncates the period
        if (endDate.isBefore(proposedDate)) {
            return endDate;
        } else {
            return proposedDate;
        }
    } else {
        return endDate;
    }
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static BigDecimal calculateProRationAfterLastBillingCycleDate(final LocalDate endDate,
        final LocalDate previousBillThroughDate, final BillingPeriod billingPeriod) {
    // Note: assumption is that previousBillThroughDate is correctly aligned with the billing cycle day
    final LocalDate nextBillThroughDate = previousBillThroughDate.plusMonths(billingPeriod.getNumberOfMonths());
    return calculateProrationBetweenDates(previousBillThroughDate, endDate, previousBillThroughDate,
            nextBillThroughDate);/*from   ww  w  .  j ava  2  s  . com*/
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateBillingCycleDateOnOrAfter(final LocalDate date,
        final int billingCycleDayLocal) {
    final int lastDayOfMonth = date.dayOfMonth().getMaximumValue();

    final LocalDate fixedDate;
    if (billingCycleDayLocal > lastDayOfMonth) {
        fixedDate = new LocalDate(date.getYear(), date.getMonthOfYear(), lastDayOfMonth, date.getChronology());
    } else {// ww  w. jav a 2s  . c o  m
        fixedDate = new LocalDate(date.getYear(), date.getMonthOfYear(), billingCycleDayLocal,
                date.getChronology());
    }

    LocalDate proposedDate = fixedDate;
    while (proposedDate.isBefore(date)) {
        proposedDate = proposedDate.plusMonths(1);
    }
    return proposedDate;
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateBillingCycleDateAfter(final LocalDate date, final int billingCycleDayLocal) {
    LocalDate proposedDate = calculateBillingCycleDateOnOrAfter(date, billingCycleDayLocal);
    if (date.compareTo(proposedDate) == 0) {
        proposedDate = proposedDate.plusMonths(1);
    }/*from   www .j a  va 2 s.c  o m*/
    return proposedDate;
}

From source file:com.ning.billing.invoice.model.InAdvanceBillingMode.java

License:Apache License

@Override
public List<RecurringInvoiceItemData> calculateInvoiceItemData(final LocalDate startDate,
        @Nullable final LocalDate endDate, final LocalDate targetDate, final int billingCycleDayLocal,
        final BillingPeriod billingPeriod) throws InvalidDateSequenceException {
    if (endDate != null && endDate.isBefore(startDate)) {
        throw new InvalidDateSequenceException();
    }/*  ww  w.j av a  2 s  .  c o m*/
    if (targetDate.isBefore(startDate)) {
        throw new InvalidDateSequenceException();
    }

    final List<RecurringInvoiceItemData> results = new ArrayList<RecurringInvoiceItemData>();

    final LocalDate firstBillingCycleDate = calculateBillingCycleDateOnOrAfter(startDate, billingCycleDayLocal);

    // We are not billing for less than a day (we could...)
    if (endDate != null && endDate.equals(startDate)) {
        return results;
    }
    //
    // If there is an endDate and that endDate is before our first coming firstBillingCycleDate, all we have to do
    // is to charge for that period
    //
    if (endDate != null && !endDate.isAfter(firstBillingCycleDate)) {
        final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate,
                endDate, billingPeriod);
        final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(startDate, endDate,
                leadingProRationPeriods);
        results.add(itemData);
        return results;
    }

    //
    // Leading proration if
    // i) The first firstBillingCycleDate is strictly after our start date AND
    // ii) The endDate is is not null and is strictly after our firstBillingCycleDate (previous check)
    //
    if (firstBillingCycleDate.isAfter(startDate)) {
        final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate,
                firstBillingCycleDate, billingPeriod);
        if (leadingProRationPeriods != null && leadingProRationPeriods.compareTo(BigDecimal.ZERO) > 0) {
            // Not common - add info in the logs for debugging purposes
            final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(startDate,
                    firstBillingCycleDate, leadingProRationPeriods);
            log.info("Adding pro-ration: {}", itemData);
            results.add(itemData);
        }
    }

    //
    // Calculate the effectiveEndDate from the firstBillingCycleDate:
    // - If endDate != null and targetDate is after endDate => this is the endDate and will lead to a trailing pro-ration
    // - If not, this is the last billingCycleDate calculation right after the targetDate
    //
    final LocalDate effectiveEndDate;
    if (endDate != null) {
        effectiveEndDate = calculateEffectiveEndDate(firstBillingCycleDate, targetDate, endDate, billingPeriod);
    } else {
        effectiveEndDate = calculateEffectiveEndDate(firstBillingCycleDate, targetDate, billingPeriod);
    }

    //
    // Based on what we calculated previously, code recompute one more time the numberOfWholeBillingPeriods
    //
    final LocalDate lastBillingCycleDate = calculateLastBillingCycleDateBefore(effectiveEndDate,
            firstBillingCycleDate, billingCycleDayLocal, billingPeriod);
    final int numberOfWholeBillingPeriods = calculateNumberOfWholeBillingPeriods(firstBillingCycleDate,
            lastBillingCycleDate, billingPeriod);
    final int numberOfMonthsPerBillingPeriod = billingPeriod.getNumberOfMonths();

    for (int i = 0; i < numberOfWholeBillingPeriods; i++) {
        final LocalDate servicePeriodStartDate;
        if (results.size() > 0) {
            // Make sure the periods align, especially with the pro-ration calculations above
            servicePeriodStartDate = results.get(results.size() - 1).getEndDate();
        } else if (i == 0) {
            // Use the specified start date
            servicePeriodStartDate = startDate;
        } else {
            throw new IllegalStateException("We should at least have one invoice item!");
        }

        // Make sure to align the end date with the BCD
        final LocalDate servicePeriodEndDate = firstBillingCycleDate
                .plusMonths((i + 1) * numberOfMonthsPerBillingPeriod);

        results.add(new RecurringInvoiceItemData(servicePeriodStartDate, servicePeriodEndDate, BigDecimal.ONE));
    }

    //
    // Now we check if indeed we need a trailing proration and add that incomplete item
    //
    if (effectiveEndDate.isAfter(lastBillingCycleDate)) {
        final BigDecimal trailingProRationPeriods = calculateProRationAfterLastBillingCycleDate(
                effectiveEndDate, lastBillingCycleDate, billingPeriod);
        if (trailingProRationPeriods.compareTo(BigDecimal.ZERO) > 0) {
            // Not common - add info in the logs for debugging purposes
            final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(lastBillingCycleDate,
                    effectiveEndDate, trailingProRationPeriods);
            log.info("Adding trailing pro-ration: {}", itemData);
            results.add(itemData);
        }
    }
    return results;
}