Example usage for org.joda.time LocalDateTime withDayOfMonth

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

Introduction

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

Prototype

public LocalDateTime withDayOfMonth(int dayOfMonth) 

Source Link

Document

Returns a copy of this datetime with the day of month field updated.

Usage

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

License:Open Source License

public LocalDateTime updateDay(LocalDateTime dateTime, String day) {
    if (!Strings.isNullOrEmpty(day)) {
        Matcher matcher = patternMonth.matcher(day);
        if (matcher.find()) {
            Integer days = Integer.parseInt(matcher.group());
            if (day.startsWith("+"))
                dateTime = dateTime.plusDays(days);
            else if (day.startsWith("-"))
                dateTime = dateTime.minusDays(days);
            else/*  w  w w.  ja  v  a  2s.c o  m*/
                dateTime = dateTime.withDayOfMonth(days);
        }
    }
    return dateTime;
}

From source file:com.effektif.workflow.api.model.NextRelativeTime.java

License:Apache License

@Override
public LocalDateTime resolve(LocalDateTime base) {
    LocalDateTime time = null;/*ww w  .  j a v a 2s  . c om*/
    if (HOUR_OF_DAY.equals(indexUnit)) {
        time = base.withTime(index, 0, 0, 0);
        if (!time.isAfter(base)) {
            return time.plusDays(1);
        }
    } else if (DAY_OF_WEEK.equals(indexUnit)) {
        time = base.withDayOfWeek(index).withTime(0, 0, 0, 0);
        if (!time.isAfter(base)) {
            time = time.plusWeeks(1);
        }
    } else if (DAY_OF_MONTH.equals(indexUnit)) {
        time = base.withDayOfMonth(index).withTime(0, 0, 0, 0);
        if (!time.isAfter(base)) {
            time = time.plusMonths(1);
        }
    }
    if (atHour != null) {
        time = time.withTime(atHour, atMinute != null ? atMinute : 0, 0, 0);
    }
    return time;
}

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

License:Open Source License

/**
 *    Leiab <tt>currentDateTime</tt> granulaarsuse <tt>superField</tt> 
 *   <i>n</i>-inda alamosa, mis vastab tingimustele <tt>subField == soughtValueOfSubField</tt>.
 *   <p>//  w  w w.j  ava 2 s . co m
 *   Negatiivsete <i>n</i> vaartuste korral voetakse alamosa "tagantpoolt": vaartus 
 *   <i>n</i> == -1 tahistab <i>viimast</i>, <i>n</i> == -2 tahistab <i>eelviimast</i>
 *   jne alamosa.
 *   <p>
 *   Praegu on defineeritud ainult <i>kuu n-inda nadalapaeva leidmise</i> operatsioon (
 *   <tt>superField == MONTH</tt>, <tt>subField == DAY_OF_WEEK</tt>, <tt>soughtValueOfSubField == a weekdayname</tt> ).
 */
public static LocalDateTime findNthSubpartOfGranularity(Granulaarsus superField, Granulaarsus subField,
        int soughtValueOfSubField, int n, LocalDateTime currentDateTime) {
    if (superField == Granulaarsus.MONTH) {
        // --------------------------------------      
        //  Kuu n-inda nadalapaeva leidmine ...
        // --------------------------------------
        if (subField == Granulaarsus.DAY_OF_WEEK && DateTimeConstants.MONDAY <= soughtValueOfSubField
                && soughtValueOfSubField <= DateTimeConstants.SUNDAY) {
            if (n > 0) {
                //
                // Algoritm:  
                //    http://msdn.microsoft.com/en-us/library/aa227532(VS.60).aspx
                //
                // Kerime kaesoleva kuu esimese kuupaeva peale ...
                LocalDateTime newDate = currentDateTime.withDayOfMonth(1);
                // Leiame esimese otsitud nadalapaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.plusDays(1);
                }
                int currentMonth = newDate.getMonthOfYear();
                newDate = newDate.plusDays((n - 1) * 7);
                if (currentMonth == newDate.getMonthOfYear()) {
                    // Kui kuu j2i kindlalt samaks, tagastame leitud nadalapaeva
                    return newDate;
                }
            } else if (n < 0) {
                // Negatiivsete vaartuste korral otsime lahendust lopust:
                // Kerime kuu viimase vaartuse peale
                LocalDateTime newDate = currentDateTime
                        .withDayOfMonth(currentDateTime.dayOfMonth().getMaximumValue());
                // Leiame viimase otsitud nadalapaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.minusDays(1);
                }
                int currentMonth = newDate.getMonthOfYear();
                newDate = newDate.minusDays(((n * (-1)) - 1) * 7);
                if (currentMonth == newDate.getMonthOfYear()) {
                    // Kui kuu j2i kindlalt samaks, tagastame leitud nadalapaeva
                    return newDate;
                }
            }
        }
        // -------------------------------------------------
        //   Kuu n-inda ndala/ndalavahetuse leidmine ...
        // -------------------------------------------------
        // -------------------------------------------------------------------
        //   Teeme eelduse, et kuu esimene ndal on ndal, mis sisaldab kuu
        //  esimest nadalapaeva {soughtValueOfSubField};
        //   Ning analoogselt, kuu viimane ndal on ndal, mis sisaldab kuu 
        //  viimast nadalapaeva {soughtValueOfSubField};
        // -------------------------------------------------------------------
        if (subField == Granulaarsus.WEEK_OF_YEAR && DateTimeConstants.MONDAY <= soughtValueOfSubField
                && soughtValueOfSubField <= DateTimeConstants.SUNDAY) {
            if (n > 0) {
                // Kerime kaesoleva kuu esimese paeva peale ...
                LocalDateTime newDate = currentDateTime.withDayOfMonth(1);
                // Leiame kuu esimese neljapaeva/laupaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.plusDays(1);
                }
                newDate = newDate.plusDays((n - 1) * 7);
                return newDate;
            } else if (n < 0) {
                // Negatiivsete vaartuste korral otsime lahendust lopust:
                // Kerime kuu viimase vaartuse peale
                LocalDateTime newDate = currentDateTime
                        .withDayOfMonth(currentDateTime.dayOfMonth().getMaximumValue());
                // Leiame viimase neljapaeva/laupaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.minusDays(1);
                }
                newDate = newDate.minusDays(((n * (-1)) - 1) * 7);
                return newDate;
            }
        }
    }
    return null;
}

From source file:org.gnucash.android.ui.chart.LineChartActivity.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
 *///  ww  w .  j ava  2s  .co m
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 = new LocalDateTime(mEarliestTimestampsMap.get(accountType));
    LocalDateTime latest = new LocalDateTime(mLatestTimestampsMap.get(accountType));
    Log.d(TAG, "Earliest " + accountType + " date " + earliest.toString("dd MM yyyy"));
    Log.d(TAG, "Latest " + accountType + " date " + latest.toString("dd MM yyyy"));
    int months = Months.monthsBetween(earliest.withDayOfMonth(1).withMillisOfDay(0),
            latest.withDayOfMonth(1).withMillisOfDay(0)).getMonths();

    int offset = getXAxisOffset(accountType);
    List<Entry> values = new ArrayList<>(months + 1);
    for (int i = 0; i < months + 1; i++) {
        long start = earliest.dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate()
                .getTime();
        long end = earliest.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();
        float balance = (float) mAccountsDbAdapter.getAccountsBalance(accountUIDList, start, end).asDouble();
        values.add(new Entry(balance, i + offset));
        Log.d(TAG, accountType + earliest.toString(" MMM yyyy") + ", balance = " + balance);
        earliest = earliest.plusMonths(1);
    }

    return values;
}

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

License:Apache License

/**
 * Calculates difference between two date values accordingly to {@code mGroupInterval}
 * @param start start date//  w ww  .j  a  va  2 s.c o  m
 * @param end end date
 * @return difference between two dates or {@code -1}
 */
private int getDateDiff(LocalDateTime start, LocalDateTime end) {
    switch (mGroupInterval) {
    case QUARTER:
        int y = Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
        return (getQuarter(end) - getQuarter(start) + y * 4);
    case MONTH:
        return Months.monthsBetween(start.withDayOfMonth(1).withMillisOfDay(0),
                end.withDayOfMonth(1).withMillisOfDay(0)).getMonths();
    case YEAR:
        return Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
    default:
        return -1;
    }
}

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

License:Apache License

/**
 * Calculates difference between two date values accordingly to {@code mGroupInterval}
 * @param start start date/* ww  w .  j  a  va 2 s  . c o  m*/
 * @param end end date
 * @return difference between two dates or {@code -1}
 */
protected int getDateDiff(LocalDateTime start, LocalDateTime end) {
    switch (mGroupInterval) {
    case QUARTER:
        int y = Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
        return getQuarter(end) - getQuarter(start) + y * 4;
    case MONTH:
        return Months.monthsBetween(start.withDayOfMonth(1).withMillisOfDay(0),
                end.withDayOfMonth(1).withMillisOfDay(0)).getMonths();
    case YEAR:
        return Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
    default:
        return -1;
    }
}