Example usage for org.joda.time DateMidnight plusMonths

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

Introduction

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

Prototype

public DateMidnight plusMonths(int months) 

Source Link

Document

Returns a copy of this date plus the specified number of months.

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 w w  w . j  av  a  2s.  c  o 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.stackframe.sarariman.ServiceAgreements.java

License:GNU General Public License

public static Collection<BilledService> getMissingBillings(Sarariman sarariman, int serviceAgreement)
        throws SQLException {
    ServiceAgreement a = ServiceAgreement.lookup(sarariman, serviceAgreement);
    if (a.getPopStart().getDayOfMonth() != 1) {
        throw new AssertionError("not expecting pop start to be on day other than start of month dom="
                + a.getPopStart().getDayOfMonth());
    }/*from  w w w .j a va2s .c  o m*/

    if (!a.getBillingPeriod().equals("monthly")) {
        throw new AssertionError("not expecting billing period other than monthly");
    }

    DateMidnight now = new DateMidnight();
    DateMidnight endOfThisMonth = new DateMidnight(now.getYear(), now.getMonthOfYear(), 1).plusMonths(1)
            .minusDays(1);
    Set<BilledService> shouldHave = new TreeSet<BilledService>();

    DateMidnight monthStart = a.getPopStart();
    while (true) {
        DateMidnight monthEnd = monthStart.plusMonths(1).minusDays(1);
        BilledService b = new BilledService(-1, a.getID(), monthStart, monthEnd, null);
        shouldHave.add(b);
        monthStart = monthStart.plusMonths(1);
        if (monthStart.isAfter(endOfThisMonth) || monthStart.isAfter(a.getPopEnd())) {
            break;
        }
    }

    List<BilledService> existing = BilledService.lookupByServiceAgreement(sarariman, serviceAgreement);
    shouldHave.removeAll(existing);
    return shouldHave;
}

From source file:ddf.metrics.plugin.webconsole.MetricsWebConsolePlugin.java

License:Open Source License

void addMonthlyReportUrls(PrintWriter pw, int numMonthlyReports, String metricsServiceUrl) {
    DateTime input = newDateTime();/*from   w  w w.  j a v a  2  s .  co m*/
    LOGGER.debug("NOW:  {}", input);

    for (int i = 1; i <= numMonthlyReports; i++) {
        try {
            DateMidnight startOfLastMonth = new DateMidnight(input.minusMonths(i).withDayOfMonth(1));
            String startDate = urlEncodeDate(startOfLastMonth);
            LOGGER.debug("Previous Month (start):  {}   (ms = {})", startDate, startOfLastMonth.getMillis());

            DateTime endOfLastMonth = startOfLastMonth.plusMonths(1).toDateTime().minus(1 /* millisecond */);
            String endDate = urlEncodeDate(endOfLastMonth);
            LOGGER.debug("Previous Month (end):  {}   (ms = {})", endOfLastMonth, endOfLastMonth.getMillis());

            startTableRow(pw, i);
            addCellLabelForRange(pw, startOfLastMonth, endOfLastMonth);
            addXLSCellLink(pw, startDate, endDate, metricsServiceUrl);
            addPPTCellLink(pw, startDate, endDate, metricsServiceUrl);
            endTableRow(pw);
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("Unsupported encoding exception adding monthly reports", e);
        }
    }
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Given a string that may be a date or a date range, extract a interval of
 * dates from that date range (ignoring time (thus the duration for the 
 * interval will be from one date midnight to another).
 * /*  ww w.  j  ava 2  s . c  o m*/
 * This probably is not the method you want - given 1950-01-05, it returns an
 * interval from midnight as the start of the 5th to midnight on the start of the 6th,
 * simply grabbing start end days from this interval will return 5 and 6, which
 * probably isn't what you are expecting.  
 * 
 * @see DateUtils#extractInterval(String) which is probably the method you want.
 * 
 * @param eventDate a string containing a dwc:eventDate from which to extract an interval.
 * @return An interval from one DateMidnight to another DateMidnight, null if no interval can be extracted.
 */
public static Interval extractDateInterval(String eventDate) {
    Interval result = null;
    DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
            DateTimeFormat.forPattern("yyyy").getParser(),
            ISODateTimeFormat.dateOptionalTimeParser().getParser() };
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
    if (eventDate != null && eventDate.contains("/") && isRange(eventDate)) {
        String[] dateBits = eventDate.split("/");
        try {
            // must be at least a 4 digit year.
            if (dateBits[0].length() > 3 && dateBits[1].length() > 3) {
                DateMidnight startDate = DateMidnight.parse(dateBits[0], formatter);
                DateMidnight endDate = DateMidnight.parse(dateBits[1], formatter);
                if (dateBits[1].length() == 4) {
                    result = new Interval(startDate, endDate.plusMonths(12).minusDays(1));
                } else if (dateBits[1].length() == 7) {
                    result = new Interval(startDate, endDate.plusMonths(1).minusDays(1));
                } else {
                    result = new Interval(startDate, endDate);
                }
            }
        } catch (Exception e) {
            // not a date range
            logger.error(e.getMessage());
        }
    } else {
        try {
            DateMidnight startDate = DateMidnight.parse(eventDate, formatter);
            logger.debug(eventDate);
            logger.debug(startDate);
            if (eventDate.length() == 4) {
                result = new Interval(startDate, startDate.plusMonths(12).minusDays(1));
            } else if (eventDate.length() == 7) {
                result = new Interval(startDate, startDate.plusMonths(1).minusDays(1));
            } else {
                result = new Interval(startDate, startDate.plusDays(1));
            }
        } catch (IllegalFieldValueException ex) {
            // can parse as a date but some token has an out of range value
            logger.debug(ex);
        } catch (Exception e) {
            // not a date
            logger.error(e.getMessage(), e);
        }
    }
    return result;
}

From source file:se.streamsource.streamflow.client.ui.workspace.table.PerspectivePeriodModel.java

License:Apache License

private String getSearchPeriod(Date fromDate, int direction, String periodName, String datePattern,
        String separator) {//from  w  w w  .ja v a  2 s  .  co  m
    DateMidnight from = new DateMidnight(fromDate);
    DateMidnight to = null;
    DateTimeFormatter format = DateTimeFormat.forPattern(datePattern);

    switch (Period.valueOf(periodName)) {
    case one_day:
        return format.print(from);

    case three_days:
        to = (direction == 1) ? from.plusDays(2) : from.minusDays(2);
        break;

    case one_week:
        to = (direction == 1) ? from.plusWeeks(1).minusDays(1) : from.minusWeeks(1).plusDays(1);
        break;

    case two_weeks:
        to = (direction == 1) ? from.plusWeeks(2).minusDays(1) : from.minusWeeks(2).plusDays(1);
        break;

    case one_month:
        to = (direction == 1) ? from.plusMonths(1).minusDays(1) : from.minusMonths(1).plusDays(1);
        break;

    case six_months:
        to = (direction == 1) ? from.plusMonths(6).minusDays(1) : from.minusMonths(6).plusDays(1);
        break;

    case one_year:
        to = (direction == 1) ? from.plusYears(1).minusDays(1) : from.minusYears(1).plusDays(1);
        break;

    }
    return (direction == 1) ? format.print(from) + separator + format.print(to)
            : format.print(to) + separator + format.print(from);

}