Example usage for org.joda.time Months Months

List of usage examples for org.joda.time Months Months

Introduction

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

Prototype

private Months(int months) 

Source Link

Document

Creates a new instance representing a number of months.

Usage

From source file:au.edu.uq.cmm.paul.servlet.WebUIController.java

License:Open Source License

private Date determineCutoff(Model model, String olderThan, String age) {

    if (olderThan.isEmpty() && age.isEmpty()) {
        model.addAttribute("message", "Either an expiry date or an age must be supplied");
        return null;
    }/*from   w  ww .j  av  a 2  s.c om*/
    String[] parts = age.split("\\s", 2);
    DateTime cutoff;
    if (olderThan.isEmpty()) {
        int value;
        try {
            value = Integer.parseInt(parts[0]);
        } catch (NumberFormatException ex) {
            model.addAttribute("message", "Age quantity is not an integer");
            return null;
        }
        BaseSingleFieldPeriod p;
        switch (parts.length == 1 ? "" : parts[1]) {
        case "minute":
        case "minutes":
            p = Minutes.minutes(value);
            break;
        case "hour":
        case "hours":
            p = Hours.hours(value);
            break;
        case "day":
        case "days":
            p = Days.days(value);
            break;
        case "week":
        case "weeks":
            p = Weeks.weeks(value);
            break;
        case "month":
        case "months":
            p = Months.months(value);
            break;
        case "year":
        case "years":
            p = Years.years(value);
            break;
        default:
            model.addAttribute("message", "Unrecognized age time-unit");
            return null;
        }
        cutoff = DateTime.now().minus(p);
    } else {
        cutoff = parseTimestamp(olderThan);
        if (cutoff == null) {
            model.addAttribute("message", "Unrecognizable expiry date");
            return null;
        }
    }
    if (cutoff.isAfter(new DateTime())) {
        model.addAttribute("message", "Supplied or computed expiry date is in the future!");
        return null;
    }
    model.addAttribute("computedDate", FORMATS[0].print(cutoff));
    return cutoff.toDate();
}

From source file:azkaban.migration.scheduler.Schedule.java

License:Apache License

public static ReadablePeriod parsePeriodString(String periodStr) {
    ReadablePeriod period;// ww  w .  j  a va 2  s.c  o  m
    char periodUnit = periodStr.charAt(periodStr.length() - 1);
    if (periodUnit == 'n') {
        return null;
    }

    int periodInt = Integer.parseInt(periodStr.substring(0, periodStr.length() - 1));
    switch (periodUnit) {
    case 'M':
        period = Months.months(periodInt);
        break;
    case 'w':
        period = Weeks.weeks(periodInt);
        break;
    case 'd':
        period = Days.days(periodInt);
        break;
    case 'h':
        period = Hours.hours(periodInt);
        break;
    case 'm':
        period = Minutes.minutes(periodInt);
        break;
    case 's':
        period = Seconds.seconds(periodInt);
        break;
    default:
        throw new IllegalArgumentException("Invalid schedule period unit '" + periodUnit);
    }

    return period;
}

From source file:azkaban.reportal.util.Reportal.java

License:Apache License

public void updateSchedules(Reportal report, ScheduleManager scheduleManager, User user, Flow flow)
        throws ScheduleManagerException {
    // Clear previous schedules
    removeSchedules(scheduleManager);//from  ww w.  j a va2 s .com
    // Add new schedule
    if (schedule) {
        int hour = (Integer.parseInt(scheduleHour) % 12) + (scheduleAmPm.equalsIgnoreCase("pm") ? 12 : 0);
        int minute = Integer.parseInt(scheduleMinute) % 60;
        DateTimeZone timeZone = scheduleTimeZone.equalsIgnoreCase("UTC") ? DateTimeZone.UTC
                : DateTimeZone.getDefault();
        DateTime firstSchedTime = DateTimeFormat.forPattern("MM/dd/yyyy").withZone(timeZone)
                .parseDateTime(scheduleDate);
        firstSchedTime = firstSchedTime.withHourOfDay(hour).withMinuteOfHour(minute).withSecondOfMinute(0)
                .withMillisOfSecond(0);

        ReadablePeriod period = null;
        if (scheduleRepeat) {
            int intervalQuantity = Integer.parseInt(scheduleIntervalQuantity);

            if (scheduleInterval.equals("y")) {
                period = Years.years(intervalQuantity);
            } else if (scheduleInterval.equals("m")) {
                period = Months.months(intervalQuantity);
            } else if (scheduleInterval.equals("w")) {
                period = Weeks.weeks(intervalQuantity);
            } else if (scheduleInterval.equals("d")) {
                period = Days.days(intervalQuantity);
            } else if (scheduleInterval.equals("h")) {
                period = Hours.hours(intervalQuantity);
            } else if (scheduleInterval.equals("M")) {
                period = Minutes.minutes(intervalQuantity);
            }
        }

        ExecutionOptions options = new ExecutionOptions();
        options.getFlowParameters().put("reportal.execution.user", user.getUserId());
        options.getFlowParameters().put("reportal.title", report.title);
        options.getFlowParameters().put("reportal.render.results.as.html",
                report.renderResultsAsHtml ? "true" : "false");
        options.setMailCreator(ReportalMailCreator.REPORTAL_MAIL_CREATOR);

        scheduleManager.scheduleFlow(-1, project.getId(), project.getName(), flow.getId(), "ready",
                firstSchedTime.getMillis(), firstSchedTime.getZone(), period, DateTime.now().getMillis(),
                firstSchedTime.getMillis(), firstSchedTime.getMillis(), user.getUserId(), options, null);
    }
}

From source file:azkaban.utils.TimeUtils.java

License:Apache License

/**
 * Parse Period String to a ReadablePeriod Object
 *
 * @param periodStr string formatted period
 * @return ReadablePeriod Object//from w w  w .  java 2 s.c  o  m
 */
public static ReadablePeriod parsePeriodString(final String periodStr) {
    final ReadablePeriod period;
    final char periodUnit = periodStr.charAt(periodStr.length() - 1);
    if (periodStr.equals("null") || periodUnit == 'n') {
        return null;
    }

    final int periodInt = Integer.parseInt(periodStr.substring(0, periodStr.length() - 1));
    switch (periodUnit) {
    case 'y':
        period = Years.years(periodInt);
        break;
    case 'M':
        period = Months.months(periodInt);
        break;
    case 'w':
        period = Weeks.weeks(periodInt);
        break;
    case 'd':
        period = Days.days(periodInt);
        break;
    case 'h':
        period = Hours.hours(periodInt);
        break;
    case 'm':
        period = Minutes.minutes(periodInt);
        break;
    case 's':
        period = Seconds.seconds(periodInt);
        break;
    default:
        throw new IllegalArgumentException("Invalid schedule period unit '" + periodUnit);
    }

    return period;
}

From source file:azkaban.utils.Utils.java

License:Apache License

public static ReadablePeriod parsePeriodString(String periodStr) {
    ReadablePeriod period;//from w w w .ja va  2 s . co  m
    char periodUnit = periodStr.charAt(periodStr.length() - 1);
    if (periodStr.equals("null") || periodUnit == 'n') {
        return null;
    }

    int periodInt = Integer.parseInt(periodStr.substring(0, periodStr.length() - 1));
    switch (periodUnit) {
    case 'y':
        period = Years.years(periodInt);
        break;
    case 'M':
        period = Months.months(periodInt);
        break;
    case 'w':
        period = Weeks.weeks(periodInt);
        break;
    case 'd':
        period = Days.days(periodInt);
        break;
    case 'h':
        period = Hours.hours(periodInt);
        break;
    case 'm':
        period = Minutes.minutes(periodInt);
        break;
    case 's':
        period = Seconds.seconds(periodInt);
        break;
    default:
        throw new IllegalArgumentException("Invalid schedule period unit '" + periodUnit);
    }

    return period;
}

From source file:com.cenrise.test.azkaban.Utils.java

License:Apache License

public static ReadablePeriod parsePeriodString(final String periodStr) {
    final ReadablePeriod period;
    final char periodUnit = periodStr.charAt(periodStr.length() - 1);
    if (periodStr.equals("null") || periodUnit == 'n') {
        return null;
    }/*w  w  w .j a  va 2 s  . co  m*/

    final int periodInt = Integer.parseInt(periodStr.substring(0, periodStr.length() - 1));
    switch (periodUnit) {
    case 'y':
        period = Years.years(periodInt);
        break;
    case 'M':
        period = Months.months(periodInt);
        break;
    case 'w':
        period = Weeks.weeks(periodInt);
        break;
    case 'd':
        period = Days.days(periodInt);
        break;
    case 'h':
        period = Hours.hours(periodInt);
        break;
    case 'm':
        period = Minutes.minutes(periodInt);
        break;
    case 's':
        period = Seconds.seconds(periodInt);
        break;
    default:
        throw new IllegalArgumentException("Invalid schedule period unit '" + periodUnit);
    }

    return period;
}

From source file:com.effektif.workflow.api.model.AfterRelativeTime.java

License:Apache License

public LocalDateTime resolve(LocalDateTime base) {
    if (this.duration == null || this.durationUnit == null) {
        return null;
    }/*from w ww  . ja  v  a  2 s.c o m*/

    ReadablePeriod period = null;
    if (DAYS.equals(durationUnit)) {
        period = Days.days(getDurationAsInt());
    } else if (WEEKS.equals(durationUnit)) {
        period = Weeks.weeks(getDurationAsInt());
    } else if (HOURS.equals(durationUnit)) {
        period = Hours.hours(getDurationAsInt());
    } else if (MONTHS.equals(durationUnit)) {
        period = Months.months(getDurationAsInt());
    } else if (YEARS.equals(durationUnit)) {
        period = Years.years(getDurationAsInt());
    } else if (MINUTES.equals(durationUnit)) {
        period = Minutes.minutes(getDurationAsInt());
    } else {
        return null;
    }

    LocalDateTime time = base.plus(period);

    if (atHour != null) {
        LocalDateTime atTime = time.withTime(atHour, atMinute != null ? atMinute : 0, 0, 0);
        if (atTime.isBefore(time)) {
            time = atTime.plusDays(1);
        } else {
            time = atTime;
        }
    } else if (isDayResolutionOrBigger()) {
        time = time.withTime(23, 59, 59, 999);
    }

    return time;
}

From source file:com.mbc.jfin.daycount.impl.calculator.ISMAActualActualDaycountCalculator.java

License:Open Source License

public double calculateDaycountFraction(SchedulePeriod schedulePeriod) {

    if (schedulePeriod.getStart().equals(schedulePeriod.getEnd()))
        return 0;

    // when the reference period is not specified, try taking
    // it equal to (d1,d2)
    LocalDate refPeriodStart = (schedulePeriod.getReferenceStart() != null ? schedulePeriod.getReferenceStart()
            : schedulePeriod.getStart());
    LocalDate refPeriodEnd = (schedulePeriod.getReferenceEnd() != null ? schedulePeriod.getReferenceEnd()
            : schedulePeriod.getEnd());//  w  ww.  jav a 2  s .  com

    LocalDate startCalendar = schedulePeriod.getStart();
    LocalDate endCalendar = schedulePeriod.getEnd();

    if (!(refPeriodEnd.isAfter(refPeriodStart) && refPeriodEnd.isAfter(startCalendar))) {
        throw new InvalidReferencePeriodException(schedulePeriod);
    }

    // estimate roughly the length in months of a period
    // Integer months =
    // Integer(0.5+12*Real(refPeriodEnd-refPeriodStart)/365);

    double monthsEstimate = DateUtils.daysBetween(refPeriodStart, refPeriodEnd) * (12.0d / 365.0d);
    int months = (int) Math.round(monthsEstimate);

    if (months == 0) {
        refPeriodStart = startCalendar;
        refPeriodEnd = startCalendar.plus(Years.ONE);
        months = 12;
    }

    double period = (double) months / 12.0;

    if (endCalendar.isBefore(refPeriodEnd) || endCalendar.equals(refPeriodEnd)) {
        if (startCalendar.isAfter(refPeriodStart) || startCalendar.equals(refPeriodStart)) {
            long numerator = DateUtils.daysBetween(startCalendar, endCalendar);
            long denominator = DateUtils.daysBetween(refPeriodStart, refPeriodEnd);

            return period * (double) numerator / (double) denominator;
        } else {

            LocalDate previousRef = startCalendar;

            //previousRef.add(Calendar.MONTH, months * -1);
            if (endCalendar.isAfter(refPeriodStart))
                return calculateDaycountFraction(
                        new SchedulePeriod(startCalendar, refPeriodStart, previousRef, refPeriodStart))
                        + calculateDaycountFraction(
                                new SchedulePeriod(refPeriodStart, endCalendar, refPeriodStart, refPeriodEnd));
            else
                return calculateDaycountFraction(
                        new SchedulePeriod(startCalendar, endCalendar, previousRef, refPeriodStart));
        }
    } else {
        if (!(refPeriodStart.isBefore(startCalendar) || refPeriodStart.equals(startCalendar))) {
            throw new InvalidReferencePeriodException(schedulePeriod);
        }

        // the part from d1 to refPeriodEnd
        double sum = calculateDaycountFraction(
                new SchedulePeriod(startCalendar, refPeriodEnd, refPeriodStart, refPeriodEnd));

        // the part from refPeriodEnd to d2
        // count how many regular periods are in [refPeriodEnd, d2],
        // then add the remaining time
        int i = 0;
        LocalDate newRefStart, newRefEnd;
        do {
            newRefStart = refPeriodEnd.plus(Months.months(months * i));
            newRefEnd = refPeriodEnd.plus(Months.months(months * (i + 1)));
            if (endCalendar.isBefore(newRefEnd)) {
                break;
            } else {
                sum += period;
                i++;
            }
        } while (true);
        double secondSum = calculateDaycountFraction(
                new SchedulePeriod(newRefStart, endCalendar, newRefStart, newRefEnd));

        sum += secondSum;
        return sum;
    }
}

From source file:com.mbc.jfin.schedule.impl.AbstractBaseScheduleGenerator.java

License:Open Source License

protected ReadablePeriod multiplyPeriod(ReadablePeriod frequency, int periodCount) throws ScheduleException {

    if (frequency instanceof Months) {
        return Months.months(frequency.getValue(0) * periodCount);
    } else if (frequency instanceof Days) {
        return Days.days(frequency.getValue(0) * periodCount);
    } else if (frequency instanceof Weeks) {
        return Weeks.weeks(frequency.getValue(0) * periodCount);
    } else if (frequency instanceof Years) {
        return Years.years(frequency.getValue(0) * periodCount);
    } else {// w ww.j  av  a  2 s.  c o  m
        throw new ScheduleException("Unknown frequency type: " + frequency);
    }
}

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

License:Apache License

public synchronized void addMonths(final int months) {
    adjustTo(Months.months(months));
}