Example usage for org.joda.time LocalDate isBefore

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

Introduction

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

Prototype

public boolean isBefore(ReadablePartial partial) 

Source Link

Document

Is this partial earlier than the specified partial.

Usage

From source file:com.mbc.jfin.daycount.impl.calculator.ISMAActualActualDaycountCalculator.java

License:Open Source License

public double calculateDaycountFraction(SchedulePeriod schedulePeriod) {

    if (schedulePeriod.getStart().equals(schedulePeriod.getEnd()))
        return 0;

    // when the reference period is not specified, try taking
    // it equal to (d1,d2)
    LocalDate refPeriodStart = (schedulePeriod.getReferenceStart() != null ? schedulePeriod.getReferenceStart()
            : schedulePeriod.getStart());
    LocalDate refPeriodEnd = (schedulePeriod.getReferenceEnd() != null ? schedulePeriod.getReferenceEnd()
            : schedulePeriod.getEnd());//  w ww  .ja v  a  2  s.  c om

    LocalDate startCalendar = schedulePeriod.getStart();
    LocalDate endCalendar = schedulePeriod.getEnd();

    if (!(refPeriodEnd.isAfter(refPeriodStart) && refPeriodEnd.isAfter(startCalendar))) {
        throw new InvalidReferencePeriodException(schedulePeriod);
    }

    // estimate roughly the length in months of a period
    // Integer months =
    // Integer(0.5+12*Real(refPeriodEnd-refPeriodStart)/365);

    double monthsEstimate = DateUtils.daysBetween(refPeriodStart, refPeriodEnd) * (12.0d / 365.0d);
    int months = (int) Math.round(monthsEstimate);

    if (months == 0) {
        refPeriodStart = startCalendar;
        refPeriodEnd = startCalendar.plus(Years.ONE);
        months = 12;
    }

    double period = (double) months / 12.0;

    if (endCalendar.isBefore(refPeriodEnd) || endCalendar.equals(refPeriodEnd)) {
        if (startCalendar.isAfter(refPeriodStart) || startCalendar.equals(refPeriodStart)) {
            long numerator = DateUtils.daysBetween(startCalendar, endCalendar);
            long denominator = DateUtils.daysBetween(refPeriodStart, refPeriodEnd);

            return period * (double) numerator / (double) denominator;
        } else {

            LocalDate previousRef = startCalendar;

            //previousRef.add(Calendar.MONTH, months * -1);
            if (endCalendar.isAfter(refPeriodStart))
                return calculateDaycountFraction(
                        new SchedulePeriod(startCalendar, refPeriodStart, previousRef, refPeriodStart))
                        + calculateDaycountFraction(
                                new SchedulePeriod(refPeriodStart, endCalendar, refPeriodStart, refPeriodEnd));
            else
                return calculateDaycountFraction(
                        new SchedulePeriod(startCalendar, endCalendar, previousRef, refPeriodStart));
        }
    } else {
        if (!(refPeriodStart.isBefore(startCalendar) || refPeriodStart.equals(startCalendar))) {
            throw new InvalidReferencePeriodException(schedulePeriod);
        }

        // the part from d1 to refPeriodEnd
        double sum = calculateDaycountFraction(
                new SchedulePeriod(startCalendar, refPeriodEnd, refPeriodStart, refPeriodEnd));

        // the part from refPeriodEnd to d2
        // count how many regular periods are in [refPeriodEnd, d2],
        // then add the remaining time
        int i = 0;
        LocalDate newRefStart, newRefEnd;
        do {
            newRefStart = refPeriodEnd.plus(Months.months(months * i));
            newRefEnd = refPeriodEnd.plus(Months.months(months * (i + 1)));
            if (endCalendar.isBefore(newRefEnd)) {
                break;
            } else {
                sum += period;
                i++;
            }
        } while (true);
        double secondSum = calculateDaycountFraction(
                new SchedulePeriod(newRefStart, endCalendar, newRefStart, newRefEnd));

        sum += secondSum;
        return sum;
    }
}

From source file:com.mbc.jfin.schedule.impl.LongFirstStubScheduleGenerator.java

License:Open Source License

public Schedule generate(LocalDate start, LocalDate end, ReadablePeriod frequency) throws ScheduleException {
    ArrayList<SchedulePeriod> schedulePeriods = new ArrayList<SchedulePeriod>();

    LocalDate holdDate = end;//from  ww w . j ava2s . c o m

    int periodCount = 1;

    while (holdDate.isAfter(start)) {
        LocalDate nextDate = end.minus(multiplyPeriod(frequency, periodCount));

        LocalDate nextDate2 = end.minus(multiplyPeriod(frequency, periodCount + 1));

        if (nextDate2.isBefore(start)) {
            SchedulePeriod schedulePeriod = new SchedulePeriod(start, holdDate, nextDate, holdDate);

            schedulePeriods.add(0, schedulePeriod);

            holdDate = nextDate2;
        } else {
            SchedulePeriod schedulePeriod = new SchedulePeriod(nextDate, holdDate);

            schedulePeriods.add(0, schedulePeriod);

            holdDate = nextDate;
        }

        periodCount++;
        if (maxPeriods > 0 && periodCount > maxPeriods) {
            throw new ScheduleTooLongException(maxPeriods);
        }
    }

    return new Schedule(schedulePeriods);
}

From source file:com.mbc.jfin.schedule.impl.LongLastStubScheduleGenerator.java

License:Open Source License

public Schedule generate(LocalDate start, LocalDate end, ReadablePeriod frequency) throws ScheduleException {
    ArrayList<SchedulePeriod> schedulePeriods = new ArrayList<SchedulePeriod>();

    LocalDate holdDate = start;

    int periodCount = 1;

    while (holdDate.isBefore(end)) {
        LocalDate nextDate = start.plus(multiplyPeriod(frequency, periodCount));

        LocalDate nextDate2 = start.plus(multiplyPeriod(frequency, periodCount + 1));

        if (nextDate2.isAfter(end)) {
            SchedulePeriod schedulePeriod = new SchedulePeriod(holdDate, end, holdDate, nextDate);

            schedulePeriods.add(schedulePeriod);

            holdDate = nextDate2;/* w ww  .ja  v  a 2 s  . c  o m*/
        } else {
            SchedulePeriod schedulePeriod = new SchedulePeriod(holdDate, nextDate);

            schedulePeriods.add(schedulePeriod);

            holdDate = nextDate;
        }

        periodCount++;
        if (maxPeriods > 0 && periodCount > maxPeriods) {
            throw new ScheduleTooLongException(maxPeriods);
        }
    }

    return new Schedule(schedulePeriods);
}

From source file:com.mbc.jfin.schedule.impl.NoStubScheduleGenerator.java

License:Open Source License

public Schedule generate(LocalDate start, LocalDate end, ReadablePeriod frequency) throws ScheduleException {
    ArrayList<SchedulePeriod> schedulePeriods = new ArrayList<SchedulePeriod>();

    LocalDate holdDate = start;

    int periodCount = 1;

    while (holdDate.isBefore(end)) {
        LocalDate nextDate = start.plus(multiplyPeriod(frequency, periodCount));

        if (nextDate.isAfter(end)) {
            throw new ScheduleWontFitException(start, end, frequency);
        }/*from w w  w  . j ava2 s. c o  m*/

        SchedulePeriod schedulePeriod = new SchedulePeriod(holdDate, nextDate);

        schedulePeriods.add(schedulePeriod);

        holdDate = nextDate;
        periodCount++;
        if (maxPeriods > 0 && periodCount > maxPeriods) {
            throw new ScheduleTooLongException(maxPeriods);
        }
    }

    return new Schedule(schedulePeriods);
}

From source file:com.mbc.jfin.schedule.impl.ShortFirstStubScheduleGenerator.java

License:Open Source License

public Schedule generate(LocalDate start, LocalDate end, ReadablePeriod frequency) throws ScheduleException {
    ArrayList<SchedulePeriod> schedulePeriods = new ArrayList<SchedulePeriod>();

    LocalDate holdDate = end;/* ww w.  j ava2 s.  com*/

    int periodCount = 1;

    while (holdDate.isAfter(start)) {
        LocalDate nextDate = end.minus(multiplyPeriod(frequency, periodCount));

        LocalDate notionalStartDate = null;
        LocalDate notionalEndDate = null;

        if (nextDate.isBefore(start)) {
            notionalStartDate = nextDate;
            notionalEndDate = holdDate;
            nextDate = start;
        }

        SchedulePeriod schedulePeriod = new SchedulePeriod(nextDate, holdDate, notionalStartDate,
                notionalEndDate);

        schedulePeriods.add(0, schedulePeriod);

        holdDate = nextDate;
        periodCount++;
        if (maxPeriods > 0 && periodCount > maxPeriods) {
            throw new ScheduleTooLongException(maxPeriods);
        }
    }

    return new Schedule(schedulePeriods);
}

From source file:com.mbc.jfin.schedule.impl.ShortLastStubScheduleGenerator.java

License:Open Source License

public Schedule generate(LocalDate start, LocalDate end, ReadablePeriod frequency) throws ScheduleException {
    ArrayList<SchedulePeriod> schedulePeriods = new ArrayList<SchedulePeriod>();

    LocalDate holdDate = start;

    int periodCount = 1;

    while (holdDate.isBefore(end)) {
        LocalDate nextDate = start.plus(multiplyPeriod(frequency, periodCount));

        LocalDate notionalStartDate = null;
        LocalDate notionalEndDate = null;

        if (nextDate.isAfter(end)) {
            notionalStartDate = holdDate;
            notionalEndDate = nextDate;//  w w  w.  j  a  va  2  s.c o  m
            nextDate = end;
        }

        SchedulePeriod schedulePeriod = new SchedulePeriod(holdDate, nextDate, notionalStartDate,
                notionalEndDate);

        schedulePeriods.add(schedulePeriod);

        holdDate = nextDate;
        periodCount++;
        if (maxPeriods > 0 && periodCount > maxPeriods) {
            throw new ScheduleTooLongException(maxPeriods);
        }
    }

    return new Schedule(schedulePeriods);
}

From source file:com.mycollab.core.utils.DateTimeUtils.java

License:Open Source License

public static LocalDate max(LocalDate... values) {
    LocalDate max = values[0];
    for (int i = 1; i < values.length; i++) {
        if (max.isBefore(values[i])) {
            max = values[i];//from   w  ww .  j a v a2 s .  c  o m
        }
    }
    return max;
}

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;/*from w w w .  j  a v  a2 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 .jav  a 2  s.  co  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;
        }//from  ww  w  . j  a v  a  2  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;
    }
}