Example usage for org.joda.time DateTime withDayOfMonth

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

Introduction

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

Prototype

public DateTime withDayOfMonth(int dayOfMonth) 

Source Link

Document

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

Usage

From source file:org.segrada.util.FlexibleDateParser.java

License:Apache License

/**
 * Parse input to Julian Day number/*from  w  w  w. ja  v a  2  s .  c om*/
 * @param input string
 * @param type calendar type, e.g. "G" or "J"
 * @param high true to get last instance of parsed time, first otherwise
 * @return julian day number
 */
public Long inputToJd(@Nullable String input, String type, boolean high) {
    // sanity check
    if (input == null || "".equals(input))
        return high ? Long.MAX_VALUE : Long.MIN_VALUE;

    try {
        DateTime date = inputFormatter.withChronology(getChronologyFromType(type))
                .withLocale(Locale.getDefault()).withZoneUTC().parseDateTime(input);

        // get last time instance of the input
        if (high) {
            // guess input pattern by counting character occurences
            int count = Math.max(StringUtils.countMatches(input, "."),
                    Math.max(StringUtils.countMatches(input, "/"), StringUtils.countMatches(input, "-")));

            if (count == 0) // year only
                date = date.withMonthOfYear(12).withDayOfMonth(31);
            else if (count == 1) { // year/month
                date = date.withDayOfMonth(date.dayOfMonth().getMaximumValue());
            } // day/month/year as is
        }

        return DateTimeUtils.toJulianDayNumber(date.getMillis());
    } catch (Exception e) {
        logger.warn("Could not parse to DateTime: " + input + " (type = " + type + ")", e);
    }
    return null;
}

From source file:ru.touchin.templates.calendar.CalendarUtils.java

License:Apache License

@NonNull
private static List<CalendarItem> fillCalendarTillCurrentDate(@NonNull final DateTime cleanStartDate,
        @NonNull final DateTime startDate) {
    DateTime temp = startDate;
    final List<CalendarItem> calendarItems = new ArrayList<>();
    int shift = 0;
    final int firstDate = temp.getDayOfMonth() - 1; //?? - 1 ?

    // add first month header
    calendarItems.add(new CalendarHeaderItem(temp.getYear(), temp.get(DateTimeFieldType.monthOfYear()) - 1,
            shift, shift)); // is Month starts from 1 or 0 ?
    temp = temp.withDayOfMonth(1);
    shift += 1;//from  w w  w  .j  a  v a2s .c om

    final int firstDayInTheWeek = temp.getDayOfWeek() - 1;

    // check if first day is Monday. If not - add empty items. Otherwise do nothing
    if (firstDayInTheWeek != 0) {
        calendarItems.add(new CalendarEmptyItem(shift, shift + firstDayInTheWeek - 1));
    }
    shift += firstDayInTheWeek;

    // add range with days before today
    calendarItems.add(new CalendarDayItem(temp.getMillis(), 1, shift, shift + firstDate - 1,
            ComparingToToday.BEFORE_TODAY));
    shift += firstDate;

    // add today item
    temp = cleanStartDate;
    calendarItems
            .add(new CalendarDayItem(temp.getMillis(), firstDate + 1, shift, shift, ComparingToToday.TODAY));

    //add empty items and header if current day the last day in the month
    if (temp.getDayOfMonth() == temp.dayOfMonth().getMaximumValue()) {
        addItemsIfCurrentDayTheLastDayInTheMonth(startDate, calendarItems);
    }

    return calendarItems;
}

From source file:shnakkydoodle.common.helpers.DateHelper.java

License:Open Source License

/**
 * Calculate the number of months between 2 dates
 * /*from w  ww  .  j  av a2  s.com*/
 * @param fromdate
 * @param todate
 * @return
 */
public static int monthsBetween(final Date futuredate, final Date pastdate) {
    if (futuredate.equals(pastdate)) {
        return 1;
    }
    DateTime startDate = new DateTime(pastdate.getTime());
    DateTime endDate = new DateTime(futuredate.getTime());
    return Months.monthsBetween(startDate.withDayOfMonth(1), endDate.withDayOfMonth(1)).getMonths();
}