Example usage for org.joda.time LocalDate getYear

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

Introduction

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

Prototype

public int getYear() 

Source Link

Document

Get the year field value.

Usage

From source file:com.axelor.apps.project.web.ProjectPlanningController.java

License:Open Source License

public void planningCurrentWeek(ActionRequest request, ActionResponse response) throws AxelorException {
    request.getContext().asType(ProjectPlanning.class);
    LocalDate currentDate = generalService.getTodayDate();
    int year = currentDate.getYear();
    int week = currentDate.getWeekOfWeekyear();
    ProjectPlanning planningCurrentWeek = null;

    planningCurrentWeek = projectPlanningRepo.all().filter("self.year = ?1 AND self.week = ?2", year, week)
            .fetchOne();//  w  w w  .j  a  v a  2  s  . co  m

    if (planningCurrentWeek == null) {

        planningCurrentWeek = projectPlanningService.createPlanning(year, week);
    }

    String type = request.getContext().get("_type").toString();
    response.setCanClose(true);
    if (type.contentEquals("user")) {
        response.setView(ActionView.define("Week" + planningCurrentWeek.getWeek())
                .model(ProjectPlanning.class.getName()).add("form", "project-my-planning-form")
                .param("forceEdit", "true").context("_showRecord", String.valueOf(planningCurrentWeek.getId()))
                .context("_type", "user").map());
    } else {
        response.setView(ActionView.define("Week" + planningCurrentWeek.getWeek())
                .model(ProjectPlanning.class.getName()).add("form", "project-my-team-planning-form")
                .param("forceEdit", "true").context("_showRecord", String.valueOf(planningCurrentWeek.getId()))
                .context("_type", "team").map());
    }
}

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   ww  w. j av  a  2 s .  c  o m*/

    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

/**
 * @deprecated analyze???/*w ww  .j av a  2  s  .c  o  m*/
 *
 * @param asOfDate
 * @param nextKMonth
 * @return
 */
public static LocalDate countDueDate(final LocalDate asOfDate, int nextKMonth) {
    final int[][] leap = { { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
            { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
    int year = asOfDate.getYear();
    int month = asOfDate.getMonthOfYear();
    int day = asOfDate.getDayOfMonth();
    int i = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 0 : 1;
    int nextYear = year + (month + nextKMonth - 1) / 12;
    int nextMonth = (month + nextKMonth - 1) % 12;
    int j = ((nextYear % 4 == 0 && nextYear % 100 != 0) || (nextYear % 400 == 0)) ? 0 : 1;
    int maxNextDay = leap[j][nextMonth];
    int maxCurrentDay = leap[i][month - 1];
    LocalDate local;
    if (day < maxCurrentDay && day < maxNextDay) {
        local = new LocalDate(nextYear, nextMonth + 1, day);
    } else {
        local = new LocalDate(nextYear, nextMonth + 1, maxNextDay);
    }
    return local;
}

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

/**
 * ???./*ww w .j  ava 2 s  .co  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.github.cassandra.jdbc.provider.datastax.CassandraPreparedStatement.java

License:Apache License

@Override
protected void setParameter(int paramIndex, Object paramValue) throws SQLException {
    String typeName = parameterMetaData.getParameterTypeName(paramIndex);
    Class javaClass = getDataTypeMappings().javaTypeFor(typeName);

    boolean replaceNullValue = this.cqlStmt.getConfiguration().replaceNullValue();

    if (javaClass != null) {
        paramValue = getDataTypeConverters().convert(paramValue, javaClass, replaceNullValue);
        // time is mapped by the driver to a primitive long, representing the number of nanoseconds since midnight
        if (CassandraDataType.TIME.getTypeName().equals(typeName) && paramValue instanceof Time) {
            Time time = (Time) paramValue;
            paramValue = new LocalTime(time).getMillisOfDay() * 1000000L;
        } else if (CassandraDataType.DATE.getTypeName().equals(typeName) && paramValue instanceof Date) {
            LocalDate localDate = LocalDate.fromDateFields((Date) paramValue);
            paramValue = com.datastax.driver.core.LocalDate.fromYearMonthDay(localDate.getYear(),
                    localDate.getMonthOfYear(), localDate.getDayOfMonth());
        } else if (CassandraDataType.BLOB.getTypeName().equals(typeName) && paramValue instanceof byte[]) {
            paramValue = ByteBuffer.wrap((byte[]) paramValue);
        }//from w  ww  .jav  a 2  s.c  o  m

        parameters.put(paramIndex, paramValue);
    } else {
        super.setParameter(paramIndex, paramValue);
    }
}

From source file:com.google.ical.compat.jodatime.LocalDateIteratorFactory.java

License:Apache License

static DateValue localDateToDateValue(LocalDate date) {
    return new DateValueImpl(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth());
}

From source file:com.google.walkaround.wave.server.googleimport.FindRemoteWavesProcessor.java

License:Open Source License

private String getQueryDateRestriction(String facet, long dateDays) {
    LocalDate date = DaysSinceEpoch.toLocalDate(dateDays);
    return String.format("%s:%04d/%02d/%02d", facet, date.getYear(), date.getMonthOfYear(),
            date.getDayOfMonth());// w ww.  j a  v  a 2s .  c  om
}

From source file:com.gs.fw.common.mithra.test.cacheloader.PYETopLevelLoaderFactory.java

License:Apache License

protected Timestamp shiftBusinessDate(Timestamp businessDate) {
    LocalDate localDate = new LocalDate(businessDate);
    int year = localDate.getYear();
    LocalDateTime pye = new LocalDateTime(year - 1, 12, 31, 23, 59, 0, 0);
    int dayOfWeek = pye.dayOfWeek().get();
    if (dayOfWeek > 5) {
        pye = pye.minusDays(dayOfWeek - 5);
    }/* www . j a va  2s.  c o  m*/
    return new Timestamp(pye.toDateTime().getMillis());
}

From source file:com.gst.infrastructure.dataqueries.service.GenericDataServiceImpl.java

License:Apache License

@Override
public String generateJsonFromGenericResultsetData(final GenericResultsetData grs) {

    final StringBuffer writer = new StringBuffer();

    writer.append("[");

    final List<ResultsetColumnHeaderData> columnHeaders = grs.getColumnHeaders();

    final List<ResultsetRowData> data = grs.getData();
    List<String> row;
    Integer rSize;//from   ww w . j  a  v a 2s  . c  om
    final String doubleQuote = "\"";
    final String slashDoubleQuote = "\\\"";
    String currColType;
    String currVal;

    for (int i = 0; i < data.size(); i++) {
        writer.append("\n{");

        row = data.get(i).getRow();
        rSize = row.size();
        for (int j = 0; j < rSize; j++) {

            writer.append(doubleQuote + columnHeaders.get(j).getColumnName() + doubleQuote + ": ");
            currColType = columnHeaders.get(j).getColumnDisplayType();
            final String colType = columnHeaders.get(j).getColumnType();
            if (currColType == null && colType.equalsIgnoreCase("INT")) {
                currColType = "INTEGER";
            }
            if (currColType == null && colType.equalsIgnoreCase("VARCHAR")) {
                currColType = "VARCHAR";
            }
            if (currColType == null && colType.equalsIgnoreCase("DATE")) {
                currColType = "DATE";
            }
            currVal = row.get(j);
            if (currVal != null && currColType != null) {
                if (currColType.equals("DECIMAL") || currColType.equals("INTEGER")) {
                    writer.append(currVal);
                } else {
                    if (currColType.equals("DATE")) {
                        final LocalDate localDate = new LocalDate(currVal);
                        writer.append("[" + localDate.getYear() + ", " + localDate.getMonthOfYear() + ", "
                                + localDate.getDayOfMonth() + "]");
                    } else if (currColType.equals("DATETIME")) {
                        final LocalDateTime localDateTime = new LocalDateTime(currVal);
                        writer.append("[" + localDateTime.getYear() + ", " + localDateTime.getMonthOfYear()
                                + ", " + localDateTime.getDayOfMonth() + " " + localDateTime.getHourOfDay()
                                + ", " + localDateTime.getMinuteOfHour() + ", "
                                + localDateTime.getSecondOfMinute() + ", " + localDateTime.getMillisOfSecond()
                                + "]");
                    } else {
                        writer.append(
                                doubleQuote + replace(currVal, doubleQuote, slashDoubleQuote) + doubleQuote);
                    }
                }
            } else {
                writer.append("null");
            }
            if (j < (rSize - 1)) {
                writer.append(",\n");
            }
        }

        if (i < (data.size() - 1)) {
            writer.append("},");
        } else {
            writer.append("}");
        }
    }

    writer.append("\n]");
    return writer.toString();

}

From source file:com.gst.portfolio.savings.domain.SavingsHelper.java

License:Apache License

private LocalDate determineInterestPostingPeriodEndDateFrom(final LocalDate periodStartDate,
        final SavingsPostingInterestPeriodType interestPostingPeriodType,
        final LocalDate interestPostingUpToDate, Integer financialYearBeginningMonth) {

    LocalDate periodEndDate = interestPostingUpToDate;
    final Integer monthOfYear = periodStartDate.getMonthOfYear();
    financialYearBeginningMonth--;/*from   w  w  w.  ja v a  2s.com*/
    if (financialYearBeginningMonth == 0)
        financialYearBeginningMonth = 12;

    final ArrayList<LocalDate> quarterlyDates = new ArrayList<>();
    quarterlyDates
            .add(periodStartDate.withMonthOfYear(financialYearBeginningMonth).dayOfMonth().withMaximumValue());
    quarterlyDates.add(periodStartDate.withMonthOfYear(financialYearBeginningMonth).plusMonths(3)
            .withYear(periodStartDate.getYear()).dayOfMonth().withMaximumValue());
    quarterlyDates.add(periodStartDate.withMonthOfYear(financialYearBeginningMonth).plusMonths(6)
            .withYear(periodStartDate.getYear()).dayOfMonth().withMaximumValue());
    quarterlyDates.add(periodStartDate.withMonthOfYear(financialYearBeginningMonth).plusMonths(9)
            .withYear(periodStartDate.getYear()).dayOfMonth().withMaximumValue());
    Collections.sort(quarterlyDates);

    final ArrayList<LocalDate> biannualDates = new ArrayList<>();
    biannualDates
            .add(periodStartDate.withMonthOfYear(financialYearBeginningMonth).dayOfMonth().withMaximumValue());
    biannualDates.add(periodStartDate.withMonthOfYear(financialYearBeginningMonth).plusMonths(6)
            .withYear(periodStartDate.getYear()).dayOfMonth().withMaximumValue());
    Collections.sort(biannualDates);
    boolean isEndDateSet = false;

    switch (interestPostingPeriodType) {
    case INVALID:
        break;
    case MONTHLY:
        // produce period end date on last day of current month
        periodEndDate = periodStartDate.dayOfMonth().withMaximumValue();
        break;
    case QUATERLY:
        for (LocalDate quarterlyDate : quarterlyDates) {
            if (quarterlyDate.isAfter(periodStartDate)) {
                periodEndDate = quarterlyDate;
                isEndDateSet = true;
                break;
            }
        }

        if (!isEndDateSet)
            periodEndDate = quarterlyDates.get(0).plusYears(1).dayOfMonth().withMaximumValue();
        break;
    case BIANNUAL:
        for (LocalDate biannualDate : biannualDates) {
            if (biannualDate.isAfter(periodStartDate)) {
                periodEndDate = biannualDate;
                isEndDateSet = true;
                break;
            }
        }

        if (!isEndDateSet)
            periodEndDate = biannualDates.get(0).plusYears(1).dayOfMonth().withMaximumValue();
        break;
    case ANNUAL:
        if (financialYearBeginningMonth < monthOfYear) {
            periodEndDate = periodStartDate.withMonthOfYear(financialYearBeginningMonth);
            periodEndDate = periodEndDate.plusYears(1);
        } else {
            periodEndDate = periodStartDate.withMonthOfYear(financialYearBeginningMonth);
        }
        periodEndDate = periodEndDate.dayOfMonth().withMaximumValue();
        break;
    }
    // interest posting always occurs on next day after the period end date.
    periodEndDate = periodEndDate.plusDays(1);
    return periodEndDate;
}