Example usage for org.joda.time LocalDate withField

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

Introduction

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

Prototype

public LocalDate withField(DateTimeFieldType fieldType, int value) 

Source Link

Document

Returns a copy of this date with the specified field set to a new value.

Usage

From source file:gg.db.datamodel.Periods.java

License:Open Source License

/**
 * Gets the adjusted start date: the date is adjusted to the first day of the period<BR/>
 * Depending on periodType, the date will be:
 * <UL>/*from ww w.j  a  v  a 2s. c  o  m*/
 * <LI>First day of the week (monday)</LI>
 * <LI>First day of the month</LI>
 * <LI>First day of the year</LI>>
 * </UL>
 * Example:
 * <UL>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.DAY) should return: <B>05/20/2006</B></LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.WEEK) should return: <B>05/15/2006</B> (if Configuration.getWeekStartingOn() == MONDAY)</LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.MONTH) should return: <B>05/01/2006</B></LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.YEAR) should return: <B>01/01/2006</B></LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.FREE) should return: <B>05/20/2006</B></LI>
 * </UL>
 * @param date Date to adjust to the start of the period
 * @param periodType Type of the Period to adjust the period
 * @return Date adjusted to the start of the period
 */
public static LocalDate getAdjustedStartDate(LocalDate date, PeriodType periodType) {
    if (date == null || periodType == null) {
        throw new IllegalArgumentException("One of the following parameters is null: 'date', 'periodType'");
    }

    LocalDate adjustedStartDate = null;
    switch (periodType) {
    case DAY: // No adjustement to do
        adjustedStartDate = date;
        break;
    case FREE: // No adjustement to do
        adjustedStartDate = date;
        break;
    case WEEK: // Adjust the date to the first day of the week (Monday)
        adjustedStartDate = date.toDateMidnight()
                .withField(DateTimeFieldType.dayOfWeek(), DateTimeConstants.MONDAY).toLocalDate();
        break;
    case MONTH: // Adjust the date to the first day of the month (<MONTH>/01/<YEAR>)
        adjustedStartDate = date.withField(DateTimeFieldType.dayOfMonth(), 1);
        break;
    case YEAR: // Adjust the date to the first day of the year (01/01/<YEAR>)
        adjustedStartDate = date.withField(DateTimeFieldType.dayOfMonth(), 1)
                .withField(DateTimeFieldType.monthOfYear(), 1);
        break;
    default:
        throw new AssertionError("Unknown PeriodType"); // should never happen
    }

    assert (adjustedStartDate != null);
    return adjustedStartDate;
}

From source file:gg.db.datamodel.Periods.java

License:Open Source License

/**
 * Gets the adjusted end date: the date is adjusted to the last day of the period<BR/>
 * Depending on periodType, the date will be:
 * <UL>//from ww  w  . jav  a  2 s.  com
 * <LI>Last day of the week (sunday)</LI>
 * <LI>Last day of the month</LI>
 * <LI>Last day of the year</LI>>
 * </UL>
 * Example:
 * <UL>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.DAY) should return: <B>05/20/2006</B></LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.WEEK) should return: <B>05/21/2006</B> (if Configuration.getWeekStartingOn() == MONDAY)</LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.MONTH) should return: <B>05/31/2006</B></LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.YEAR) should return: <B>12/31/2006</B></LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.FREE) should return: <B>05/20/2006</B></LI>
 * </UL>
 * @param date Date to adjust to the end of the period
 * @param periodType Type of the Period to adjust the date
 * @return Date adjusted to the end of the period
 */
public static LocalDate getAdjustedEndDate(LocalDate date, PeriodType periodType) {
    if (date == null || periodType == null) {
        throw new IllegalArgumentException("One of the following parameters is null: 'date', 'periodType'");
    }

    LocalDate adjustedEndDate = null;
    switch (periodType) {
    case DAY: // No adjustement to do
        adjustedEndDate = date;
        break;
    case FREE: // No adjustement to do
        adjustedEndDate = date;
        break;
    case WEEK: // Adjust the date to the last day of the week (sunday)
        adjustedEndDate = date.toDateMidnight()
                .withField(DateTimeFieldType.dayOfWeek(), DateTimeConstants.MONDAY + 6).toLocalDate();
        break;
    case MONTH: // Adjust the date to the last day of the month
        adjustedEndDate = date.withField(DateTimeFieldType.dayOfMonth(), 1).plusMonths(1).minusDays(1);
        break;
    case YEAR: // Adjust the date to the last day of the year (12/31/<YEAR>)
        adjustedEndDate = date.withField(DateTimeFieldType.dayOfMonth(), 1).plusMonths(1).minusDays(1)
                .withField(DateTimeFieldType.monthOfYear(), 12);
        break;
    default:
        throw new AssertionError("Unknown PeriodType"); // should never happen
    }

    assert (adjustedEndDate != null);
    return adjustedEndDate;
}

From source file:org.fenixedu.treasury.domain.tariff.InterestRate.java

License:Open Source License

public InterestRateBean calculateInterest(final Map<LocalDate, BigDecimal> amountInDebtMap,
        final Map<LocalDate, BigDecimal> createdInterestEntries, final LocalDate dueDate,
        final LocalDate paymentDate) {

    TreeMap<LocalDate, BigDecimal> sortedMap = new TreeMap<LocalDate, BigDecimal>();
    sortedMap.putAll(amountInDebtMap);// ww w  .  j av a 2 s  . c o m

    if (getInterestType().isGlobalRate()
            && !GlobalInterestRate.findUniqueByYear(dueDate.getYear()).get().isApplyPaymentMonth()) {
        final LocalDate lastDayForInterestCalculation = paymentDate.withField(DateTimeFieldType.dayOfMonth(), 1)
                .minusDays(1);

        for (final LocalDate localDate : Sets.newTreeSet(sortedMap.keySet())) {
            if (localDate.isAfter(lastDayForInterestCalculation)) {
                sortedMap.remove(localDate);
            }
        }

        sortedMap.put(lastDayForInterestCalculation, BigDecimal.ZERO);
    } else {
        sortedMap.put(paymentDate, BigDecimal.ZERO);
    }

    sortedMap = splitDatesWithYearsSpan(sortedMap);

    if (getInterestType().isFixedAmount()) {
        return calculedForFixedAmount();
    }

    if (getInterestType().isDaily() || getInterestType().isGlobalRate()) {
        return calculateDaily(createdInterestEntries, dueDate, paymentDate, sortedMap);
    }

    if (getInterestType().isMonthly()) {
        return calculateMonthly(createdInterestEntries, dueDate, paymentDate, sortedMap);
    }

    throw new RuntimeException("unknown interest type formula");
}