Example usage for org.joda.time LocalDateTime plusYears

List of usage examples for org.joda.time LocalDateTime plusYears

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime plusYears.

Prototype

public LocalDateTime plusYears(int years) 

Source Link

Document

Returns a copy of this datetime plus the specified number of years.

Usage

From source file:com.axelor.csv.script.ImportDateTime.java

License:Open Source License

public LocalDateTime updateYear(LocalDateTime dateTime, String year) {
    if (!Strings.isNullOrEmpty(year)) {
        Matcher matcher = patternYear.matcher(year);
        if (matcher.find()) {
            Integer years = Integer.parseInt(matcher.group());
            if (year.startsWith("+"))
                dateTime = dateTime.plusYears(years);
            else if (year.startsWith("-"))
                dateTime = dateTime.minusYears(years);
            else/*from w  w  w .  j a  v a  2 s  .c  om*/
                dateTime = dateTime.withYear(years);
        }
    }
    return dateTime;
}

From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.SemLeidmiseAbimeetodid.java

License:Open Source License

/**
 *   Rakendab yldistatud Baldwini akent, et leida hetkele <tt>currentDateTime</tt> l2himat 
 *  ajahetke, mis vastab tingimustele <tt>field == soughtValue</tt>. Kui tingimustele vastav
 *  hetk j22b v2lja Baldwini akna raamidest, toimib kui tavaline SET operatsioon, omistades
 *  <tt>field := soughtValue</tt> ajahetke <tt>currentDateTime</tt> raames.
 *  <p>/*  ww  w .j ava 2  s. c o m*/
 *  Praegu on implementeeritud ainult granulaarsuste 
 *  <tt>DAY_OF_WEEK</tt>, <tt>MONTH</tt>, <tt>YEAR_OF_CENTURY</tt>  
 *  toetus. 
    *  <p>
 *  <i>What's the Date? High Accuracy Interpretation of Weekday Name,</i> Dale, Mazur (2009)
 */
public static LocalDateTime applyBaldwinWindow(Granulaarsus field, LocalDateTime currentDateTime,
        int soughtValue) {
    // ---------------------------------
    //  DAY_OF_WEEK
    // ---------------------------------      
    if (field == Granulaarsus.DAY_OF_WEEK && DateTimeConstants.MONDAY <= soughtValue
            && soughtValue <= DateTimeConstants.SUNDAY) {
        int currentDayOfWeek = currentDateTime.getDayOfWeek();
        int addToCurrent = 0;
        // 1) Vaatame eelnevat 3-e p&auml;eva
        while (addToCurrent > -4) {
            if (currentDayOfWeek == soughtValue) {
                return currentDateTime.plusDays(addToCurrent);
            }
            currentDayOfWeek--;
            if (currentDayOfWeek < DateTimeConstants.MONDAY) {
                currentDayOfWeek = DateTimeConstants.SUNDAY;
            }
            addToCurrent--;
        }
        // 2) Vaatame jargnevat 3-e p&auml;eva
        currentDayOfWeek = currentDateTime.getDayOfWeek();
        addToCurrent = 0;
        while (addToCurrent < 4) {
            if (currentDayOfWeek == soughtValue) {
                return currentDateTime.plusDays(addToCurrent);
            }
            currentDayOfWeek++;
            if (currentDayOfWeek > DateTimeConstants.SUNDAY) {
                currentDayOfWeek = DateTimeConstants.MONDAY;
            }
            addToCurrent++;
        }
    }
    // ---------------------------------
    //  MONTH
    // ---------------------------------
    if (field == Granulaarsus.MONTH && DateTimeConstants.JANUARY <= soughtValue
            && soughtValue <= DateTimeConstants.DECEMBER) {
        int currentMonth = currentDateTime.getMonthOfYear();
        int addToCurrent = 0;
        // 1) Vaatame eelnevat 5-e kuud
        while (addToCurrent > -6) {
            if (currentMonth == soughtValue) {
                return currentDateTime.plusMonths(addToCurrent);
            }
            currentMonth--;
            if (currentMonth < DateTimeConstants.JANUARY) {
                currentMonth = DateTimeConstants.DECEMBER;
            }
            addToCurrent--;
        }
        // 2) Vaatame jargnevat 5-e kuud
        currentMonth = currentDateTime.getMonthOfYear();
        addToCurrent = 0;
        while (addToCurrent < 6) {
            if (currentMonth == soughtValue) {
                return currentDateTime.plusMonths(addToCurrent);
            }
            currentMonth++;
            if (currentMonth > DateTimeConstants.DECEMBER) {
                currentMonth = DateTimeConstants.JANUARY;
            }
            addToCurrent++;
        }
        // Kui otsitav kuu j2i aknast v2lja, k2sitleme seda kui "selle aasta" otsitud kuud
        return currentDateTime.withMonthOfYear(soughtValue);
    }
    // ---------------------------------
    //  YEAR_OF_CENTURY
    // ---------------------------------
    if (field == Granulaarsus.YEAR_OF_CENTURY && 0 <= soughtValue && soughtValue <= 99) {
        // API tunnistab vrtuseid vahemikust 1 kuni 100
        if (soughtValue == 0) {
            soughtValue = 100;
        }
        int currentYear = currentDateTime.getYearOfCentury();
        int addToCurrent = 0;
        // 1) Vaatame eelnevat 4-a aastakymmet 
        while (addToCurrent > -49) {
            if (currentYear == soughtValue) {
                return currentDateTime.plusYears(addToCurrent);
            }
            currentYear--;
            if (currentYear < 1) {
                currentYear = 100;
            }
            addToCurrent--;
        }
        // 2) Vaatame jargnevat 4-a aastakymmet
        currentYear = currentDateTime.getYearOfCentury();
        addToCurrent = 0;
        while (addToCurrent < 49) {
            if (currentYear == soughtValue) {
                return currentDateTime.plusYears(addToCurrent);
            }
            currentYear++;
            if (currentYear > 100) {
                currentYear = 1;
            }
            addToCurrent++;
        }
        // Kui otsitav kuu j2i aknast v2lja, k2sitleme seda kui "selle sajandi" otsitud aastat
        return currentDateTime.withYearOfCentury(soughtValue);
    }
    return currentDateTime;
}

From source file:me.vertretungsplan.parser.ParserUtils.java

License:Mozilla Public License

static LocalDateTime parseDateTime(String string) {
    if (string == null)
        return null;
    reinitIfNeeded();/*  w  ww .  j  av  a  2s.co m*/

    string = string.replace("Stand:", "").replace("Import:", "").trim();
    int i = 0;
    for (DateTimeFormatter f : dateTimeFormatters) {
        try {
            LocalDateTime dt = f.parseLocalDateTime(string);
            if (dateTimeFormats[i].contains("yyyy")) {
                return dt;
            } else {
                Duration currentYearDifference = abs(new Duration(DateTime.now(), dt.toDateTime()));
                Duration lastYearDifference = abs(new Duration(DateTime.now(), dt.minusYears(1).toDateTime()));
                Duration nextYearDifference = abs(new Duration(DateTime.now(), dt.plusYears(1).toDateTime()));
                if (lastYearDifference.isShorterThan(currentYearDifference)) {
                    return DateTimeFormat.forPattern(dateTimeFormats[i]).withLocale(Locale.GERMAN)
                            .withDefaultYear(f.getDefaultYear() - 1).parseLocalDateTime(string);
                } else if (nextYearDifference.isShorterThan(currentYearDifference)) {
                    return DateTimeFormat.forPattern(dateTimeFormats[i]).withLocale(Locale.GERMAN)
                            .withDefaultYear(f.getDefaultYear() + 1).parseLocalDateTime(string);
                } else {
                    return dt;
                }
            }
        } catch (IllegalArgumentException e) {
            // Does not match this format, try the next one
        }
        i++;
    }
    // Does not match any known format :(
    return null;
}

From source file:org.apache.isis.core.metamodel.facets.value.datetimejodalocal.JodaLocalDateTimeUtil.java

License:Apache License

private static LocalDateTime add(final LocalDateTime original, final int years, final int months,
        final int days, final int hours, final int minutes) {
    return original.plusYears(years).plusMonths(months).plusDays(days).plusHours(hours).plusMinutes(minutes);
}

From source file:org.apache.isis.core.progmodel.facets.value.datetimejodalocal.JodaLocalDateTimeValueSemanticsProvider.java

License:Apache License

@Override
protected LocalDateTime add(final LocalDateTime original, final int years, final int months, final int days,
        final int hours, final int minutes) {
    if (hours != 0 || minutes != 0) {
        throw new IllegalArgumentException("cannot add non-zero hours or minutes to a LocalDateTime");
    }/*from   ww  w. j a  va  2s  .  com*/
    return original.plusYears(years).plusMonths(months).plusDays(days);
}

From source file:org.gnucash.android.model.Recurrence.java

License:Apache License

/**
 * Sets the end time of this recurrence by specifying the number of occurences
 * @param numberOfOccurences Number of occurences from the start time
 *//*from ww w.j av  a  2  s  .  c  o m*/
public void setPeriodEnd(int numberOfOccurences) {
    LocalDateTime localDate = new LocalDateTime(mPeriodStart.getTime());
    LocalDateTime endDate;
    int occurrenceDuration = numberOfOccurences * mPeriodType.getMultiplier();
    switch (mPeriodType) {
    case DAY:
        endDate = localDate.plusDays(occurrenceDuration);
        break;
    case WEEK:
        endDate = localDate.plusWeeks(occurrenceDuration);
        break;
    default:
    case MONTH:
        endDate = localDate.plusMonths(occurrenceDuration);
        break;
    case YEAR:
        endDate = localDate.plusYears(occurrenceDuration);
        break;
    }
    mPeriodEnd = new Timestamp(endDate.toDateTime().getMillis());
}

From source file:org.gnucash.android.ui.report.barchart.StackedBarChartFragment.java

License:Apache License

/**
 * Returns a data object that represents a user data of the specified account types
 * @return a {@code BarData} instance that represents a user data
 *//*  w  w w.j a va2s .c o  m*/
protected BarData getData() {
    List<BarEntry> values = new ArrayList<>();
    List<String> labels = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();
    Map<String, Integer> accountToColorMap = new LinkedHashMap<>();
    List<String> xValues = new ArrayList<>();
    LocalDateTime tmpDate = new LocalDateTime(getStartDate(mAccountType).toDate().getTime());
    int count = getDateDiff(new LocalDateTime(getStartDate(mAccountType).toDate().getTime()),
            new LocalDateTime(getEndDate(mAccountType).toDate().getTime()));
    for (int i = 0; i <= count; i++) {
        long start = 0;
        long end = 0;
        switch (mGroupInterval) {
        case MONTH:
            start = tmpDate.dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
            end = tmpDate.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            xValues.add(tmpDate.toString(X_AXIS_MONTH_PATTERN));
            tmpDate = tmpDate.plusMonths(1);
            break;
        case QUARTER:
            int quarter = getQuarter(tmpDate);
            start = tmpDate.withMonthOfYear(quarter * 3 - 2).dayOfMonth().withMinimumValue().millisOfDay()
                    .withMinimumValue().toDate().getTime();
            end = tmpDate.withMonthOfYear(quarter * 3).dayOfMonth().withMaximumValue().millisOfDay()
                    .withMaximumValue().toDate().getTime();

            xValues.add(String.format(X_AXIS_QUARTER_PATTERN, quarter, tmpDate.toString(" YY")));
            tmpDate = tmpDate.plusMonths(3);
            break;
        case YEAR:
            start = tmpDate.dayOfYear().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
            end = tmpDate.dayOfYear().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            xValues.add(tmpDate.toString(X_AXIS_YEAR_PATTERN));
            tmpDate = tmpDate.plusYears(1);
            break;
        }
        List<Float> stack = new ArrayList<>();
        for (Account account : mAccountsDbAdapter.getSimpleAccountList()) {
            if (account.getAccountType() == mAccountType && !account.isPlaceholderAccount()
                    && account.getCommodity().equals(mCommodity)) {

                double balance = mAccountsDbAdapter
                        .getAccountsBalance(Collections.singletonList(account.getUID()), start, end).asDouble();
                if (balance != 0) {
                    stack.add((float) balance);

                    String accountName = account.getName();
                    while (labels.contains(accountName)) {
                        if (!accountToColorMap.containsKey(account.getUID())) {
                            for (String label : labels) {
                                if (label.equals(accountName)) {
                                    accountName += " ";
                                }
                            }
                        } else {
                            break;
                        }
                    }
                    labels.add(accountName);

                    if (!accountToColorMap.containsKey(account.getUID())) {
                        Integer color;
                        if (mUseAccountColor) {
                            color = (account.getColor() != Account.DEFAULT_COLOR) ? account.getColor()
                                    : COLORS[accountToColorMap.size() % COLORS.length];
                        } else {
                            color = COLORS[accountToColorMap.size() % COLORS.length];
                        }
                        accountToColorMap.put(account.getUID(), color);
                    }
                    colors.add(accountToColorMap.get(account.getUID()));

                    Log.d(TAG, mAccountType + tmpDate.toString(" MMMM yyyy ") + account.getName() + " = "
                            + stack.get(stack.size() - 1));
                }
            }
        }

        String stackLabels = labels.subList(labels.size() - stack.size(), labels.size()).toString();
        values.add(new BarEntry(floatListToArray(stack), i, stackLabels));
    }

    BarDataSet set = new BarDataSet(values, "");
    set.setDrawValues(false);
    set.setStackLabels(labels.toArray(new String[labels.size()]));
    set.setColors(colors);

    if (set.getYValueSum() == 0) {
        mChartDataPresent = false;
        return getEmptyData();
    }
    mChartDataPresent = true;
    return new BarData(xValues, set);
}

From source file:org.gnucash.android.ui.report.BarChartFragment.java

License:Apache License

/**
 * Returns a data object that represents a user data of the specified account types
 * @return a {@code BarData} instance that represents a user data
 *//*from w w w .j a  v a  2s  .  c  om*/
private BarData getData() {
    List<BarEntry> values = new ArrayList<>();
    List<String> labels = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();
    Map<String, Integer> accountToColorMap = new LinkedHashMap<>();
    List<String> xValues = new ArrayList<>();
    LocalDateTime tmpDate = new LocalDateTime(getStartDate(mAccountType).toDate().getTime());
    int count = getDateDiff(new LocalDateTime(getStartDate(mAccountType).toDate().getTime()),
            new LocalDateTime(getEndDate(mAccountType).toDate().getTime()));
    for (int i = 0; i <= count; i++) {
        long start = 0;
        long end = 0;
        switch (mGroupInterval) {
        case MONTH:
            start = tmpDate.dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
            end = tmpDate.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            xValues.add(tmpDate.toString(X_AXIS_MONTH_PATTERN));
            tmpDate = tmpDate.plusMonths(1);
            break;
        case QUARTER:
            int quarter = getQuarter(tmpDate);
            start = tmpDate.withMonthOfYear(quarter * 3 - 2).dayOfMonth().withMinimumValue().millisOfDay()
                    .withMinimumValue().toDate().getTime();
            end = tmpDate.withMonthOfYear(quarter * 3).dayOfMonth().withMaximumValue().millisOfDay()
                    .withMaximumValue().toDate().getTime();

            xValues.add(String.format(X_AXIS_QUARTER_PATTERN, quarter, tmpDate.toString(" YY")));
            tmpDate = tmpDate.plusMonths(3);
            break;
        case YEAR:
            start = tmpDate.dayOfYear().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
            end = tmpDate.dayOfYear().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            xValues.add(tmpDate.toString(X_AXIS_YEAR_PATTERN));
            tmpDate = tmpDate.plusYears(1);
            break;
        }
        List<Float> stack = new ArrayList<>();
        for (Account account : mAccountsDbAdapter.getSimpleAccountList()) {
            if (account.getAccountType() == mAccountType && !account.isPlaceholderAccount()
                    && account.getCurrency() == mCurrency) {

                double balance = mAccountsDbAdapter
                        .getAccountsBalance(Collections.singletonList(account.getUID()), start, end).asDouble();
                if (balance != 0) {
                    if (!accountToColorMap.containsKey(account.getUID())) {
                        Integer color;
                        if (mUseAccountColor) {
                            color = (account.getColorHexCode() != null)
                                    ? Color.parseColor(account.getColorHexCode())
                                    : COLORS[accountToColorMap.size() % COLORS.length];
                        } else {
                            color = COLORS[accountToColorMap.size() % COLORS.length];
                        }
                        accountToColorMap.put(account.getUID(), color);
                    }

                    stack.add((float) balance);
                    labels.add(account.getName());
                    colors.add(accountToColorMap.get(account.getUID()));
                    Log.d(TAG, mAccountType + tmpDate.toString(" MMMM yyyy ") + account.getName() + " = "
                            + stack.get(stack.size() - 1));
                }
            }
        }

        String stackLabels = labels.subList(labels.size() - stack.size(), labels.size()).toString();
        values.add(new BarEntry(floatListToArray(stack), i, stackLabels));
    }

    BarDataSet set = new BarDataSet(values, "");
    set.setDrawValues(false);
    set.setStackLabels(labels.toArray(new String[labels.size()]));
    set.setColors(colors);

    if (set.getYValueSum() == 0) {
        mChartDataPresent = false;
        return getEmptyData();
    }
    mChartDataPresent = true;
    return new BarData(xValues, set);
}

From source file:org.gnucash.android.ui.report.linechart.CashFlowLineChartFragment.java

License:Apache License

/**
 * Returns entries which represent a user data of the specified account type
 * @param accountType account's type which user data will be processed
 * @return entries which represent a user data
 *///from w  ww . j  av  a 2s. c  o m
private List<Entry> getEntryList(AccountType accountType) {
    List<String> accountUIDList = new ArrayList<>();
    for (Account account : mAccountsDbAdapter.getSimpleAccountList()) {
        if (account.getAccountType() == accountType && !account.isPlaceholderAccount()
                && account.getCommodity().equals(mCommodity)) {
            accountUIDList.add(account.getUID());
        }
    }

    LocalDateTime earliest;
    LocalDateTime latest;
    if (mReportPeriodStart == -1 && mReportPeriodEnd == -1) {
        earliest = new LocalDateTime(mEarliestTimestampsMap.get(accountType));
        latest = new LocalDateTime(mLatestTimestampsMap.get(accountType));
    } else {
        earliest = new LocalDateTime(mReportPeriodStart);
        latest = new LocalDateTime(mReportPeriodEnd);
    }
    Log.d(TAG, "Earliest " + accountType + " date " + earliest.toString("dd MM yyyy"));
    Log.d(TAG, "Latest " + accountType + " date " + latest.toString("dd MM yyyy"));

    int xAxisOffset = getDateDiff(new LocalDateTime(mEarliestTransactionTimestamp), earliest);
    int count = getDateDiff(earliest, latest);
    List<Entry> values = new ArrayList<>(count + 1);
    for (int i = 0; i <= count; i++) {
        long start = 0;
        long end = 0;
        switch (mGroupInterval) {
        case QUARTER:
            int quarter = getQuarter(earliest);
            start = earliest.withMonthOfYear(quarter * 3 - 2).dayOfMonth().withMinimumValue().millisOfDay()
                    .withMinimumValue().toDate().getTime();
            end = earliest.withMonthOfYear(quarter * 3).dayOfMonth().withMaximumValue().millisOfDay()
                    .withMaximumValue().toDate().getTime();

            earliest = earliest.plusMonths(3);
            break;
        case MONTH:
            start = earliest.dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate()
                    .getTime();
            end = earliest.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            earliest = earliest.plusMonths(1);
            break;
        case YEAR:
            start = earliest.dayOfYear().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
            end = earliest.dayOfYear().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            earliest = earliest.plusYears(1);
            break;
        }
        float balance = (float) mAccountsDbAdapter.getAccountsBalance(accountUIDList, start, end).asDouble();
        values.add(new Entry(balance, i + xAxisOffset));
        Log.d(TAG, accountType + earliest.toString(" MMM yyyy") + ", balance = " + balance);

    }

    return values;
}

From source file:org.gnucash.android.ui.report.LineChartFragment.java

License:Apache License

/**
 * Returns entries which represent a user data of the specified account type
 * @param accountType account's type which user data will be processed
 * @return entries which represent a user data
 *//*from  w w w .  j a  va2s .com*/
private List<Entry> getEntryList(AccountType accountType) {
    List<String> accountUIDList = new ArrayList<>();
    for (Account account : mAccountsDbAdapter.getSimpleAccountList()) {
        if (account.getAccountType() == accountType && !account.isPlaceholderAccount()
                && account.getCurrency() == mCurrency) {
            accountUIDList.add(account.getUID());
        }
    }

    LocalDateTime earliest;
    LocalDateTime latest;
    if (mReportStartTime == -1 && mReportEndTime == -1) {
        earliest = new LocalDateTime(mEarliestTimestampsMap.get(accountType));
        latest = new LocalDateTime(mLatestTimestampsMap.get(accountType));
    } else {
        earliest = new LocalDateTime(mReportStartTime);
        latest = new LocalDateTime(mReportEndTime);
    }
    Log.d(TAG, "Earliest " + accountType + " date " + earliest.toString("dd MM yyyy"));
    Log.d(TAG, "Latest " + accountType + " date " + latest.toString("dd MM yyyy"));

    int xAxisOffset = getDateDiff(new LocalDateTime(mEarliestTransactionTimestamp), earliest);
    int count = getDateDiff(earliest, latest);
    List<Entry> values = new ArrayList<>(count + 1);
    for (int i = 0; i <= count; i++) {
        long start = 0;
        long end = 0;
        switch (mGroupInterval) {
        case QUARTER:
            int quarter = getQuarter(earliest);
            start = earliest.withMonthOfYear(quarter * 3 - 2).dayOfMonth().withMinimumValue().millisOfDay()
                    .withMinimumValue().toDate().getTime();
            end = earliest.withMonthOfYear(quarter * 3).dayOfMonth().withMaximumValue().millisOfDay()
                    .withMaximumValue().toDate().getTime();

            earliest = earliest.plusMonths(3);
            break;
        case MONTH:
            start = earliest.dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate()
                    .getTime();
            end = earliest.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            earliest = earliest.plusMonths(1);
            break;
        case YEAR:
            start = earliest.dayOfYear().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
            end = earliest.dayOfYear().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

            earliest = earliest.plusYears(1);
            break;
        }
        float balance = (float) mAccountsDbAdapter.getAccountsBalance(accountUIDList, start, end).asDouble();
        values.add(new Entry(balance, i + xAxisOffset));
        Log.d(TAG, accountType + earliest.toString(" MMM yyyy") + ", balance = " + balance);

    }

    return values;
}