Example usage for org.joda.time DateMidnight withDayOfMonth

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

Introduction

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

Prototype

public DateMidnight withDayOfMonth(int dayOfMonth) 

Source Link

Document

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

Usage

From source file:com.latlab.common.model.PeriodUtils.java

/**
 * Obtains a Map of {@link YearQuarterDate} for all the quarters of the
 * specified year. If <code>year</code> value is 0 or less, it is assumed
 * that the year is the current year./*from  ww  w.j  ava  2  s .co  m*/
 *
 * @param year
 * @return
 */
public static Map<Period, DateRange> getQuarterDates(int year) {
    Map<Period, DateRange> quarterMap = new HashMap<>();

    DateMidnight refDate = new DateMidnight();
    if (year > 0) {
        refDate = refDate.withYear(year);
    }

    refDate = refDate.withMonthOfYear(1).withDayOfMonth(1);
    Date startDate1 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate1 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();

    DateRange quarterDate = new DateRange(Period.FIRST_QUARTER, year, startDate1, endDate1);
    quarterMap.put(quarterDate.getPeriod(), quarterDate);

    refDate = refDate.withMonthOfYear(4).withDayOfMonth(1);
    Date starteDate2 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate2 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();
    DateRange quarterDate2 = new DateRange(Period.SECOND_QUARTER, year, starteDate2, endDate2);
    quarterMap.put(quarterDate2.getPeriod(), quarterDate2);

    refDate = refDate.withMonthOfYear(7).withDayOfMonth(1);
    Date starteDate3 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate3 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();
    DateRange quarterDate3 = new DateRange(Period.THIRD_QUARTER, year, starteDate3, endDate3);
    quarterMap.put(quarterDate3.getPeriod(), quarterDate3);

    refDate = refDate.withMonthOfYear(10).withDayOfMonth(1);
    Date starteDate4 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate4 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();
    DateRange quarterDate4 = new DateRange(Period.LAST_QUARTER, refDate.getYear(), starteDate4, endDate4);
    quarterMap.put(quarterDate4.getPeriod(), quarterDate4);
    return quarterMap;
}

From source file:com.manydesigns.portofino.calendar.AbstractMonth.java

License:Open Source License

public AbstractMonth(DateMidnight referenceDateMidnight) {
    logger.debug("Initializing month");
    this.referenceDateMidnight = referenceDateMidnight;
    logger.debug("Reference date midnight: {}", referenceDateMidnight);

    monthStart = referenceDateMidnight.withDayOfMonth(1);
    monthEnd = monthStart.plusMonths(1);
    monthInterval = new Interval(monthStart, monthEnd);
    logger.debug("Month interval: {}", monthInterval);

    daysCount = Days.daysIn(monthInterval).getDays();
    logger.debug("Initializing {} days", daysCount);

    days = createDaysArray(daysCount);//from   w w w  .  j  a v a2s  .  c om
    DateMidnight dayStart = monthStart;
    for (int i = 0; i < daysCount; i++) {
        DateMidnight dayEnd = dayStart.plusDays(1);
        days[i] = createDay(dayStart, dayEnd);

        // advance to next day
        dayStart = dayEnd;
    }
}