Example usage for org.joda.time DateMidnight withYear

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

Introduction

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

Prototype

public DateMidnight withYear(int year) 

Source Link

Document

Returns a copy of this date with the year 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 a v  a  2 s .c  om*/
 *
 * @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;
}