Example usage for org.joda.time DateTime plusMonths

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

Introduction

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

Prototype

public DateTime plusMonths(int months) 

Source Link

Document

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

Usage

From source file:com.ning.billing.catalog.DefaultDuration.java

License:Apache License

@Override
public DateTime addToDateTime(final DateTime dateTime) {
    if ((number == null) && (unit != TimeUnit.UNLIMITED)) {
        return dateTime;
    }//from  w  ww .  j  a v a 2s .c  o m

    switch (unit) {
    case DAYS:
        return dateTime.plusDays(number);
    case MONTHS:
        return dateTime.plusMonths(number);
    case YEARS:
        return dateTime.plusYears(number);
    case UNLIMITED:
        return dateTime.plusYears(100);
    default:
        return dateTime;
    }
}

From source file:com.ning.billing.util.clock.DefaultClock.java

License:Apache License

public static DateTime addOrRemoveDuration(final DateTime input, final List<Duration> durations,
        final boolean add) {
    DateTime result = input;
    for (final Duration cur : durations) {
        switch (cur.getUnit()) {
        case DAYS:
            result = add ? result.plusDays(cur.getNumber()) : result.minusDays(cur.getNumber());
            break;

        case MONTHS:
            result = add ? result.plusMonths(cur.getNumber()) : result.minusMonths(cur.getNumber());
            break;

        case YEARS:
            result = add ? result.plusYears(cur.getNumber()) : result.minusYears(cur.getNumber());
            break;
        case UNLIMITED:
        default:/*ww w. java2s . c o  m*/
            throw new RuntimeException("Trying to move to unlimited time period");
        }
    }
    return result;
}

From source file:com.ning.billing.util.clock.OldClockMock.java

License:Apache License

private DateTime adjustFromDuration(final DateTime input) {

    DateTime result = input;
    for (final Duration cur : deltaFromRealityDuration) {
        switch (cur.getUnit()) {
        case DAYS:
            result = result.plusDays(cur.getNumber());
            break;

        case MONTHS:
            result = result.plusMonths(cur.getNumber());
            break;

        case YEARS:
            result = result.plusYears(cur.getNumber());
            break;

        case UNLIMITED:
        default:/*  ww  w . j av  a 2 s . c  o m*/
            throw new RuntimeException("ClockMock is adjusting an unlimited time period");
        }
    }
    if (deltaFromRealityDurationEpsilon != 0) {
        result = result.plus(deltaFromRealityDurationEpsilon);
    }
    return result;
}

From source file:com.norconex.collector.http.recrawl.impl.GenericRecrawlableResolver.java

License:Apache License

private boolean isRecrawlableFromFrequency(SitemapChangeFrequency cf, PreviousCrawlData prevData,
        String context) {//  w  w w  . j  a v  a2  s.  co  m
    if (cf == null) {
        return true;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("The " + context + " change frequency is " + cf + " for: " + prevData.getReference());
    }
    if (cf == SitemapChangeFrequency.ALWAYS) {
        return true;
    }
    if (cf == SitemapChangeFrequency.NEVER) {
        return false;
    }

    DateTime minCrawlDate = new DateTime(prevData.getCrawlDate());
    switch (cf) {
    case HOURLY:
        minCrawlDate = minCrawlDate.plusHours(1);
        break;
    case DAILY:
        minCrawlDate = minCrawlDate.plusDays(1);
        break;
    case WEEKLY:
        minCrawlDate = minCrawlDate.plusWeeks(1);
        break;
    case MONTHLY:
        minCrawlDate = minCrawlDate.plusMonths(1);
        break;
    case YEARLY:
        minCrawlDate = minCrawlDate.plusYears(1);
        break;
    default:
        break;
    }

    if (minCrawlDate.isBeforeNow()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Recrawl suggested according to " + context
                    + " directive (change frequency < elapsed time since " + prevData.getCrawlDate() + ") for: "
                    + prevData.getReference());
        }
        return true;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("No recrawl suggested according to " + context
                + " directive (change frequency >= elapsed time since " + prevData.getCrawlDate() + ") for: "
                + prevData.getReference());
    }
    return false;
}

From source file:com.pacoapp.paco.shared.scheduling.NonESMSignalGenerator.java

License:Open Source License

private DateTime getNextScheduleDay(DateTime midnightTomorrow) {

    switch (schedule.getScheduleType()) {
    case Schedule.DAILY:
        return nextRepeatDaily(midnightTomorrow);

    case Schedule.WEEKDAY:
        int tomorrowDOW = midnightTomorrow.getDayOfWeek();
        if (tomorrowDOW > DateTimeConstants.FRIDAY) {
            return midnightTomorrow.plusDays(8 - tomorrowDOW);
        } else {//from   w ww. j a v  a 2 s .com
            return midnightTomorrow;
        }

    case Schedule.WEEKLY:
        int scheduleDays = schedule.getWeekDaysScheduled();
        if (scheduleDays == 0) {
            return null;
        }
        for (int i = 0; i < 8; i++) { // go at least to the same day next week.
            int midnightTomorrowDOW = midnightTomorrow.getDayOfWeek();
            Integer nowDowIndex = Schedule.DAYS_OF_WEEK[midnightTomorrowDOW == 7 ? 0 : midnightTomorrowDOW]; // joda is 1 based & counts Monday as first day of the week so Sunday is 7 instead of 0.
            if ((scheduleDays & nowDowIndex) == nowDowIndex) {
                return nextRepeatWeekly(midnightTomorrow);
            }
            midnightTomorrow = midnightTomorrow.plusDays(1);

        }
        throw new IllegalStateException("Cannot get to here. Weekly must repeat at least once a week");

    case Schedule.MONTHLY:
        if (schedule.getByDayOfMonth()) {
            int midnightDOM = midnightTomorrow.getDayOfMonth();
            int scheduledDOM = schedule.getDayOfMonth();
            if (midnightDOM == scheduledDOM) {
                return midnightTomorrow;
            } else if (midnightDOM > scheduledDOM) {
                MutableDateTime mutableDateTime = midnightTomorrow.plusMonths(1).toMutableDateTime();
                mutableDateTime.setDayOfMonth(scheduledDOM);
                return nextRepeatMonthly(mutableDateTime.toDateTime());
            } else {
                return nextRepeatMonthly(midnightTomorrow.plusDays(scheduledDOM - midnightDOM));
            }
        } else {
            Integer nthOfMonth = schedule.getNthOfMonth();
            Integer dow = getDOWFromIndexedValue(); // only one selection, so take log2 to get index of dow
            DateMidnight nthDowDate = getNthDOWOfMonth(midnightTomorrow, nthOfMonth, dow).toDateMidnight();
            DateTime returnDate = null;
            if (nthDowDate.equals(midnightTomorrow)) {
                returnDate = midnightTomorrow;
            } else if (nthDowDate.isAfter(midnightTomorrow)) {
                returnDate = nthDowDate.toDateTime();
            } else {
                returnDate = getNthDOWOfMonth(midnightTomorrow.plusMonths(1), nthOfMonth, dow).toDateTime();
            }
            return nextRepeatMonthly(returnDate);
        }
    default:
        throw new IllegalStateException("Schedule has an unknown type: " + schedule.getScheduleType());
    }
}

From source file:com.qcadoo.view.internal.components.ganttChart.GanttChartScaleImpl.java

License:Open Source License

public boolean isTooLargeRange() {
    DateTime dateTimeFrom = new DateTime(dateFrom);
    DateTime dateTimeTo = new DateTime(dateTo);
    return (dateTimeFrom.plusMonths(zoomLevel.getMaxRangeInMonths()).compareTo(dateTimeTo) < 0);
}

From source file:com.quinsoft.zeidon.domains.DateTimeDomain.java

License:Open Source License

private Object addWithContext(Task task, AttributeInstance attributeInstance, AttributeDef attributeDef,
        Object currentValue, Object operand, String contextName) {
    assert !StringUtils.isBlank(contextName);

    if (operand instanceof AttributeInstance)
        operand = ((AttributeInstance) operand).getValue();

    if (!(operand instanceof Integer) && !(operand instanceof Long)) {
        throw new ZeidonException(
                "When adding to DateTime with a context, operand must be integer or long value.  "
                        + "Type of operand = %s",
                operand.getClass().getName()).prependAttributeDef(attributeDef);
    }//from  ww w . j  av a2 s . c  o  m

    int value = ((Number) operand).intValue();
    DateTime dt = (DateTime) currentValue;

    switch (contextName.toLowerCase()) {
    case "day":
    case "days":
        return dt.plusDays(value);

    case "hour":
    case "hours":
        return dt.plusHours(value);

    case "minute":
    case "minutes":
        return dt.plusMinutes(value);

    case "milli":
    case "millis":
    case "millisecond":
    case "milliseconds":
        return dt.plus(((Number) operand).longValue());

    case "month":
    case "months":
        return dt.plusMonths(value);

    case "second":
    case "seconds":
        return dt.plusSeconds(value);

    case "week":
    case "weeks":
        return dt.plusWeeks(value);

    case "year":
    case "years":
        return dt.plusYears(value);
    }

    // TODO Auto-generated method stub
    throw new ZeidonException("Unknown context name '%s' for DateTime domain", contextName)
            .prependAttributeDef(attributeDef);
}

From source file:com.quinsoft.zeidon.vml.VmlOperation.java

License:Open Source License

protected int AddToAttributeFromVariable(View view, String entityName, String attributeName, Object value,
        char variableType, int maxLth, String contextName) {
    int nRC = 0;//from w w w  . j av  a  2s . c  o  m
    EntityCursor cursor = view.cursor(entityName);
    if (cursor.isNull()) {
        nRC = zCALL_ERROR;
    } else {
        if (contextName.equals("Month")) {
            int nMonths = ((Number) value).intValue();
            DateTime date = view.cursor(entityName).getAttribute(attributeName).getDateTime();
            DateTime date2 = date.plusMonths(nMonths);
            view.cursor(entityName).getAttribute(attributeName).setValue(date2);
        } else if (contextName.equals("Day")) {
            int nDays = ((Number) value).intValue();
            DateTime date = view.cursor(entityName).getAttribute(attributeName).getDateTime();
            DateTime date2 = date.plusDays(nDays);
            view.cursor(entityName).getAttribute(attributeName).setValue(date2);
        } else {
            cursor.getAttribute(attributeName).add(value);
        }
    }

    return nRC;
}

From source file:com.salesmanBuddy.dao.JDBCSalesmanBuddyDAO.java

License:Open Source License

private String wrapReportContentWithBeginningEnd(String content, ReportBeginEnd beginning,
        ReportBeginEnd ending, Integer dealershipId, DateTime from, DateTime to) {

    boolean useDefaultEnding = false;
    String timePeriod = "";
    if (to.isAfter(from.plusMonths(1)))
        timePeriod = "Monthly ";
    else if (to.isAfter(from.plusWeeks(1)))
        timePeriod = "Weekly ";
    else if (to.isAfter(from.plusDays(1)))
        timePeriod = "Daily ";

    StringBuilder sb = new StringBuilder();
    Dealerships dealership = this.getDealershipById(dealershipId);
    String fromTo = new StringBuilder().append(this.printTimeDateForReports(from)).append(" to ")
            .append(this.printTimeDateForReports(to)).toString();
    switch (beginning) {
    case AllSalesmen:
        sb.append("All Salesmen ").append(timePeriod).append("summary report for ").append(dealership.getName())
                .append(" from ").append(fromTo).append(".\n\n");
        sb.append(content);//from ww  w  . j  a va2 s .c o  m
        useDefaultEnding = true;
        break;

    case DealershipSummary:
        sb.append("Dealership-Wide ").append(timePeriod).append("summary report for ")
                .append(dealership.getName()).append(" from ").append(fromTo).append(".\n\n");
        sb.append(content);
        useDefaultEnding = true;
        break;

    case StockNumberSummary:
        sb.append("Stock Number ").append(timePeriod).append("summary report for ").append(dealership.getName())
                .append(" from ").append(fromTo).append(".\n\n");
        sb.append(content);
        useDefaultEnding = true;
        break;

    case TestDriveNow:
        sb.append(content);
        useDefaultEnding = true;
        break;

    case TestDriveSummary:
        sb.append("Test Drive ").append(timePeriod).append("summary report for ").append(dealership.getName())
                .append(" from ").append(fromTo).append(".\n\n");
        sb.append(content);
        useDefaultEnding = true;
        break;

    case Warnings:
        sb.append("Warnings ").append(timePeriod).append("report for ").append(dealership.getName())
                .append(" from ").append(fromTo).append(".\n\n");
        sb.append(content);
        useDefaultEnding = true;
        break;

    default:
        break;

    }

    if (useDefaultEnding)
        sb.append("\n\nThank you for using Salesman Buddy. If you have any questions, contact us at ")
                .append(SUPPORT_EMAIL);

    return sb.toString();
}

From source file:com.sonicle.webtop.calendar.Service.java

License:Open Source License

public void processPrintScheduler(HttpServletRequest request, HttpServletResponse response) {
    ByteArrayOutputStream baos = null;
    UserProfile up = getEnv().getProfile();

    try {//  www.j  av a2 s.com
        String filename = ServletUtils.getStringParameter(request, "filename", "print");
        String view = ServletUtils.getStringParameter(request, "view", "w5");
        String from = ServletUtils.getStringParameter(request, "startDate", true);

        DateTime startDate = DateTimeUtils.parseYmdHmsWithZone(from, "00:00:00", up.getTimeZone());

        ReportConfig.Builder builder = reportConfigBuilder();
        DateTime fromDate = null, toDate = null;
        AbstractAgenda rpt = null;
        if (view.equals("d")) {
            fromDate = startDate.withTimeAtStartOfDay();
            toDate = startDate.plusDays(1).withTimeAtStartOfDay();
            rpt = new RptAgendaSummary(builder.build(), 1);

        } else if (view.equals("w5")) {
            fromDate = startDate.withTimeAtStartOfDay();
            toDate = startDate.plusDays(5).withTimeAtStartOfDay();
            rpt = new RptAgendaWeek5(builder.build());

        } else if (view.equals("w")) {
            fromDate = startDate.withTimeAtStartOfDay();
            toDate = startDate.plusDays(7).withTimeAtStartOfDay();
            rpt = new RptAgendaWeek7(builder.build());

        } else if (view.equals("dw")) {
            fromDate = startDate.withTimeAtStartOfDay();
            toDate = startDate.plusDays(14).withTimeAtStartOfDay();
            rpt = new RptAgendaSummary(builder.build(), 14);

        } else if (view.equals("m")) {
            if (startDate.getDayOfMonth() == 1) {
                fromDate = startDate.withTimeAtStartOfDay();
            } else {
                fromDate = startDate.plusMonths(1).withDayOfMonth(1).withTimeAtStartOfDay();
            }
            int days = fromDate.dayOfMonth().getMaximumValue();
            toDate = fromDate.plusMonths(1).withDayOfMonth(1).withTimeAtStartOfDay();
            rpt = new RptAgendaSummary(builder.build(), days);

        } else {
            throw new WTException("View not supported [{0}]", view);
        }

        Set<Integer> activeCalIds = getActiveFolderIds();
        Map<Integer, Calendar> calendars = folders.entrySet().stream()
                .filter(map -> activeCalIds.contains(map.getKey()))
                .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue().getCalendar()));

        List<SchedEventInstance> instances = manager.listEventInstances(activeCalIds,
                new DateTimeRange(fromDate, toDate), up.getTimeZone(), true);
        rpt.setDataSource(manager, fromDate, toDate, up.getTimeZone(), calendars, instances);

        baos = new ByteArrayOutputStream();
        WT.generateReportToStream(rpt, AbstractReport.OutputType.PDF, baos);
        ServletUtils.setContentDispositionHeader(response, "inline", filename + ".pdf");
        ServletUtils.writeContent(response, baos, "application/pdf");

    } catch (Exception ex) {
        logger.error("Error in PrintScheduler", ex);
        ServletUtils.writeErrorHandlingJs(response, ex.getMessage());
    } finally {
        IOUtils.closeQuietly(baos);
    }
}