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.sos.scheduler.model.objects.JodaTools.java

License:Apache License

public static DateTime getWeekdayInIntervalOrNull(Interval interval, int weekday, int which) {
    DateTime currentDate = getStartOfMonth(interval.getStart());
    DateTime result = getWeekdayInMonth(currentDate, weekday, which);
    while (!interval.contains(result)) {
        currentDate = currentDate.plusMonths(1);
        result = getWeekdayInMonth(currentDate, weekday, which);
        if (!result.isBefore(interval.getEnd()))
            return null;
    }//w  ww. j  ava  2  s.c  o  m
    return result;
}

From source file:com.sos.scheduler.model.objects.JodaTools.java

License:Apache License

public static DateTime getDayInIntervalOrNull(Interval interval, int day) {
    DateTime currentDate = getStartOfMonth(interval.getStart());
    DateTime result = getDayInMonth(currentDate, day);
    while (!interval.contains(result)) {
        currentDate = currentDate.plusMonths(1);
        result = getDayInMonth(currentDate, day);
        if (!result.isBefore(interval.getEnd()))
            return null;
    }/* ww w  . j  a  v a 2  s  .c  om*/
    return result;
}

From source file:com.sos.scheduler.model.objects.JodaTools.java

License:Apache License

public static DateTime getEndOfMonth(DateTime base) {
    return getStartOfMonth(base.plusMonths(1)).minusMillis(1);
}

From source file:com.sos.scheduler.model.objects.JSObjMonthdaysDay.java

License:Apache License

@Override
public RunTimeElements getRunTimeElements(Interval timeRange) {
    RunTimeElements result = new RunTimeElements(timeRange);
    Iterator<Integer> it = getDay().iterator();
    while (it.hasNext()) {
        int day = it.next();
        DateTime date = JodaTools.getDayInIntervalOrNull(timeRange, day);
        while (date != null) {
            Iterator<Period> itP = getPeriod().iterator();
            while (itP.hasNext()) {
                Period p = itP.next();
                JSObjPeriod period = new JSObjPeriod(objFactory);
                period.setObjectFieldsFrom(p);
                DateTime start = period.getDtSingleStartOrNull(date);
                if (start != null && timeRange.contains(start))
                    result.add(new RunTimeElement(start, period.getWhenHoliday()));
            }//www. ja v a 2s .  c  om
            DateTime start = JodaTools.getStartOfMonth(date.plusMonths(1));
            if (!timeRange.contains(start))
                break;
            Interval i = new Interval(start, timeRange.getEnd());
            date = JodaTools.getDayInIntervalOrNull(i, day);
        }
    }
    //         Collections.sort(result, DateTimeComparator.getInstance());
    return result;
}

From source file:com.sos.scheduler.model.objects.JSObjUltimosDay.java

License:Apache License

@Override
public RunTimeElements getRunTimeElements(Interval timeRange) {
    RunTimeElements result = new RunTimeElements(timeRange);
    Iterator<Integer> it = getDay().iterator();
    while (it.hasNext()) {
        int day = it.next();
        DateTime date = JodaTools.getDayInIntervalOrNull(timeRange, (day * -1));
        while (date != null) {
            Iterator<Period> itP = getPeriod().iterator();
            while (itP.hasNext()) {
                Period p = itP.next();
                JSObjPeriod period = new JSObjPeriod(objFactory);
                period.setObjectFieldsFrom(p);
                DateTime start = period.getDtSingleStartOrNull(date);
                if (start != null && timeRange.contains(start))
                    result.add(new RunTimeElement(start, period.getWhenHoliday()));
            }/*from   w  ww . j a  va 2 s.  c  o  m*/
            DateTime start = JodaTools.getStartOfMonth(date.plusMonths(1));
            if (!timeRange.contains(start))
                break;
            Interval i = new Interval(start, timeRange.getEnd());
            date = JodaTools.getDayInIntervalOrNull(i, day);
        }
    }
    //         Collections.sort(result, DateTimeComparator.getInstance());
    return result;
}

From source file:com.sos.scheduler.model.objects.JSObjWeekday.java

License:Apache License

@Override
public RunTimeElements getRunTimeElements(Interval timeRange) {
    RunTimeElements result = new RunTimeElements(timeRange);
    int which = Integer.valueOf(getWhich());
    Iterator<String> it = getDay().iterator();
    while (it.hasNext()) {
        String dayList = it.next();
        List<Integer> weekdays = JodaTools.getJodaWeekdays(dayList);
        for (int weekday : weekdays) {
            DateTime d = JodaTools.getWeekdayInIntervalOrNull(timeRange, weekday, which);
            while (d != null) {
                Iterator<Period> itP = getPeriod().iterator();
                while (itP.hasNext()) {
                    Period p = itP.next();
                    JSObjPeriod period = new JSObjPeriod(objFactory);
                    period.setObjectFieldsFrom(p);
                    DateTime start = period.getDtSingleStartOrNull(d);
                    if (start != null && timeRange.contains(start))
                        result.add(new RunTimeElement(start, period.getWhenHoliday()));
                }/*from w w w . j  a v a  2 s  . c o m*/
                DateTime start = JodaTools.getStartOfMonth(d.plusMonths(1));
                if (!timeRange.contains(start))
                    break;
                Interval i = new Interval(start, timeRange.getEnd());
                d = JodaTools.getWeekdayInIntervalOrNull(i, weekday, which);
            }
        }
    }
    //      Collections.sort(result, DateTimeComparator.getInstance());
    return result;
}

From source file:com.splicemachine.derby.utils.SpliceDateFunctions.java

License:Apache License

public static Date ADD_MONTHS(Date source, int numOfMonths) {
    if (source == null)
        return null;
    DateTime dt = new DateTime(source);
    return new Date(dt.plusMonths(numOfMonths).getMillis());
}

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java

License:Open Source License

private IntervalleObject alignPastInterval(IntervalleObject presentInterval, IntervalleObject pastInterval,
        Axis joinAxis) throws ScopeException {

    if (joinAxis != null && presentInterval != null && pastInterval != null) {

        Object lowerPresent = presentInterval.getLowerBound();
        Object lowerPast = pastInterval.getLowerBound();
        Object upperPresent = presentInterval.getUpperBound();
        Object upperPast = pastInterval.getUpperBound();
        ////from w  w w  . j  a va 2  s  .  com
        IDomain image = joinAxis.getDefinition().getImageDomain();
        if (lowerPresent instanceof Date && lowerPast instanceof Date) {

            DateTime lowerPastDT = new DateTime((Date) lowerPast);
            DateTime lowerPresentDT = new DateTime((Date) lowerPresent);
            DateTime upperPresentDT = new DateTime((Date) upperPresent);
            DateTime upperPastDT = new DateTime((Date) upperPast);

            // realign
            if (image.isInstanceOf(IDomain.YEARLY)) {
                // check if present is an exact number of years
                if (lowerPresentDT.getDayOfYear() == 1
                        && upperPresentDT.getDayOfYear() == upperPresentDT.dayOfYear().getMaximumValue()) {
                    // check of both periods have the same number of days
                    Period presentPeriod = new Period(new LocalDate(lowerPresent),
                            (new LocalDate(upperPresent)), PeriodType.days());
                    Period pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)),
                            PeriodType.days());
                    if (presentPeriod.getDays() == pastPeriod.getDays()) {
                        presentPeriod = new Period(new LocalDate(lowerPresent),
                                (new LocalDate(upperPresent)).plusDays(1), PeriodType.years());
                        pastPeriod = new Period(new LocalDate(lowerPast),
                                (new LocalDate(upperPast)).plusDays(1), PeriodType.years());

                        // realign
                        if (presentPeriod.getYears() > pastPeriod.getYears()) {
                            // some days are missing to align the periods
                            if (lowerPastDT.getDayOfYear() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(upperPastDT.getYear(), 1, 1, 0, 0).toDate();
                                return new IntervalleObject(newLowerPast, upperPast);
                            }
                            if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) {
                                // year over year
                                Date newUpperPast = new DateTime(upperPastDT.getYear(), 12, 31, 23, 59)
                                        .toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);
                            }
                        } else {
                            // either already aligned, or some days should
                            // be removed

                            if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) {
                                // year over Year
                                Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59)
                                        .toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);

                            }
                            if (lowerPastDT.getDayOfYear() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0)
                                        .toDate();
                                return new IntervalleObject(newLowerPast, upperPast);
                            }

                        }
                    }
                }
            } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) {
                // check if present is an exact number of month
                if (lowerPresentDT.getDayOfMonth() == 1
                        && upperPresentDT.getDayOfMonth() == upperPresentDT.dayOfMonth().getMaximumValue()) {
                    // check of both periods have the same number of days
                    Period presentPeriod = new Period(new LocalDate(lowerPresent), new LocalDate(upperPresent),
                            PeriodType.days());
                    Period pastPeriod = new Period(new LocalDate(lowerPast), new LocalDate(upperPast),
                            PeriodType.days());
                    if (presentPeriod.getDays() == pastPeriod.getDays()) {
                        // realign
                        presentPeriod = new Period(new LocalDate(lowerPresent),
                                (new LocalDate(upperPresent)).plusDays(1), PeriodType.months());
                        pastPeriod = new Period(new LocalDate(lowerPast),
                                (new LocalDate(upperPast)).plusDays(1), PeriodType.months());
                        if (presentPeriod.getMonths() > pastPeriod.getMonths()) {
                            // some days are missing

                            if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) {
                                // month over month
                                Date newUpperPast = new DateTime(upperPastDT.getYear(),
                                        upperPastDT.getMonthOfYear(),
                                        upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);
                            }

                            if (lowerPastDT.getDayOfMonth() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(lowerPastDT.getYear(),
                                        lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate();
                                return new IntervalleObject(newLowerPast, upperPast);

                            }

                        } else {
                            // either already aligned, of some days should
                            // be removed
                            if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) {
                                /// month over month
                                if (upperPastDT.getMonthOfYear() == 1) {
                                    Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59)
                                            .toDate();
                                    return new IntervalleObject(lowerPast, newUpperPast);

                                } else {

                                    upperPastDT = upperPastDT.minusMonths(1);
                                    Date newUpperPast = new DateTime(upperPastDT.getYear(),
                                            upperPastDT.getMonthOfYear(),
                                            upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate();
                                    return new IntervalleObject(lowerPast, newUpperPast);
                                }
                            }
                            if (lowerPastDT.getDayOfMonth() != 1) {
                                // previous period
                                if (lowerPastDT.getMonthOfYear() == 12) {
                                    Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0)
                                            .toDate();
                                    return new IntervalleObject(newLowerPast, upperPast);

                                } else {
                                    lowerPastDT = lowerPastDT.plusMonths(1);
                                    Date newLowerPast = new DateTime(lowerPastDT.getYear(),
                                            lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate();
                                    return new IntervalleObject(newLowerPast, upperPast);

                                }

                            }

                        }
                    }
                }
            }
        }
    }
    return pastInterval;
}

From source file:com.streamsets.pipeline.lib.parser.syslog.SyslogParser.java

License:Apache License

/**
 * Parse the RFC3164 date format. This is trickier than it sounds because this
 * format does not specify a year so we get weird edge cases at year
 * boundaries. This implementation tries to "do what I mean".
 * @param ts RFC3164-compatible timestamp to be parsed
 * @return Typical (for Java) milliseconds since the UNIX epoch
 *///  w ww . j a v  a 2s. c o m
protected long parseRfc3164Time(String ts) throws OnRecordErrorException {
    DateTime now = DateTime.now();
    int year = now.getYear();
    ts = TWO_SPACES.matcher(ts).replaceFirst(" ");
    DateTime date;
    try {
        date = rfc3164Format.parseDateTime(ts);
    } catch (IllegalArgumentException e) {
        throw new OnRecordErrorException(Errors.SYSLOG_10, ts, e);
    }
    // try to deal with boundary cases, i.e. new year's eve.
    // rfc3164 dates are really dumb.
    // NB: cannot handle replaying of old logs or going back to the future
    DateTime fixed = date.withYear(year);
    // flume clock is ahead or there is some latency, and the year rolled
    if (fixed.isAfter(now) && fixed.minusMonths(1).isAfter(now)) {
        fixed = date.withYear(year - 1);
        // flume clock is behind and the year rolled
    } else if (fixed.isBefore(now) && fixed.plusMonths(1).isBefore(now)) {
        fixed = date.withYear(year + 1);
    }
    date = fixed;
    return date.getMillis();
}

From source file:com.streamsets.pipeline.lib.parser.udp.syslog.SyslogParser.java

License:Apache License

/**
 * Parse the RFC3164 date format. This is trickier than it sounds because this
 * format does not specify a year so we get weird edge cases at year
 * boundaries. This implementation tries to "do what I mean".
 * @param ts RFC3164-compatible timestamp to be parsed
 * @return Typical (for Java) milliseconds since the UNIX epoch
 *//*ww  w.j a  v  a  2 s.co  m*/
protected long parseRfc3164Time(String recordIdentifer, String msg, String ts) throws OnRecordErrorException {
    DateTime now = DateTime.now();
    int year = now.getYear();
    ts = TWO_SPACES.matcher(ts).replaceFirst(" ");
    DateTime date;
    try {
        date = rfc3164Format.parseDateTime(ts);
    } catch (IllegalArgumentException e) {
        throw throwOnRecordErrorException(recordIdentifer, msg, Errors.SYSLOG_10, ts, e);
    }
    // try to deal with boundary cases, i.e. new year's eve.
    // rfc3164 dates are really dumb.
    // NB: cannot handle replaying of old logs or going back to the future
    DateTime fixed = date.withYear(year);
    // flume clock is ahead or there is some latency, and the year rolled
    if (fixed.isAfter(now) && fixed.minusMonths(1).isAfter(now)) {
        fixed = date.withYear(year - 1);
        // flume clock is behind and the year rolled
    } else if (fixed.isBefore(now) && fixed.plusMonths(1).isBefore(now)) {
        fixed = date.withYear(year + 1);
    }
    date = fixed;
    return date.getMillis();
}