Example usage for org.joda.time Days Days

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

Introduction

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

Prototype

private Days(int days) 

Source Link

Document

Creates a new instance representing a number of days.

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  w w  .  j  a  v a 2s .co  m
    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.app.jmx.JobScheduler.java

License:Apache License

public String scheduleWorkflow(String jobName, boolean ignoreDeps, int hour, int minutes, int seconds,
        String scheduledDate, boolean isRecurring, int period, String periodUnits) {
    String errorMsg = null;/*  w  w w  . j a va  2 s  .  co  m*/
    if (jobName == null || jobName.trim().length() == 0) {
        errorMsg = "You must select at least one job to run.";
        logger.error(errorMsg);
        return errorMsg;
    }
    JobDescriptor descriptor = jobManager.getJobDescriptor(jobName);
    if (descriptor == null) {
        errorMsg = "Job: '" + jobName + "' doesn't exist.";
        logger.error(errorMsg);
        return errorMsg;
    }

    DateTime day = null;
    DateTime time = null;
    try {
        if (scheduledDate == null || scheduledDate.trim().length() == 0) {
            day = new LocalDateTime().toDateTime();
            time = day.withHourOfDay(hour).withMinuteOfHour(minutes).withSecondOfMinute(seconds);
            if (day.isAfter(time)) {
                time = time.plusDays(1);
            }
        } else {
            try {
                day = DateTimeFormat.forPattern("MM-dd-yyyy").parseDateTime(scheduledDate);
            } catch (IllegalArgumentException e) {
                logger.error(e);
                return "Invalid date: '" + scheduledDate + "', \"MM-dd-yyyy\" format is expected.";
            }
            time = day.withHourOfDay(hour).withMinuteOfHour(minutes).withSecondOfMinute(seconds);
        }
    } catch (IllegalFieldValueException e) {
        logger.error(e);
        return "Invalid schedule time (see logs): " + e.getMessage();

    }
    ReadablePeriod thePeriod = null;
    if (isRecurring) {
        if ("d".equals(periodUnits)) {
            thePeriod = Days.days(period);
        } else if ("h".equals(periodUnits)) {
            thePeriod = Hours.hours(period);
        } else if ("m".equals(periodUnits)) {
            thePeriod = Minutes.minutes(period);
        } else if ("s".equals(periodUnits)) {
            thePeriod = Seconds.seconds(period);
        } else {
            errorMsg = "Unknown period unit: " + periodUnits;
            logger.error(errorMsg);
            return errorMsg;
        }
    }
    try {
        if (thePeriod == null) {
            scheduler.schedule(jobName, time, ignoreDeps);
        } else {
            scheduler.schedule(jobName, time, thePeriod, ignoreDeps);
        }
        return "Schedule Successful!";
    } catch (Exception e) {
        logger.error(e);
        return "Schedule Failed (see logs): " + e.getMessage();
    }
}

From source file:azkaban.app.Scheduler.java

License:Apache License

private ReadablePeriod parsePeriodString(String jobname, String periodStr) {
    ReadablePeriod period;//from w w w  .  j  ava2  s  . co 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 '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 + "' for job " + jobname);
    }

    return period;
}

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

License:Apache License

public static ReadablePeriod parsePeriodString(String periodStr) {
    ReadablePeriod period;/*from  w  ww  .  j av a  2s .c om*/
    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);/*w w w  .j  a  v a2s.  c o m*/
    // 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/* ww  w  .j a  v  a  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;//ww  w. j  ava2 s  .  c o  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:azkaban.web.pages.IndexServlet.java

License:Apache License

private ReadablePeriod parsePeriod(HttpServletRequest req) throws ServletException {
    int period = getIntParam(req, "period");
    String periodUnits = getParam(req, "period_units");
    if ("d".equals(periodUnits))
        return Days.days(period);
    else if ("h".equals(periodUnits))
        return Hours.hours(period);
    else if ("m".equals(periodUnits))
        return Minutes.minutes(period);
    else if ("s".equals(periodUnits))
        return Seconds.seconds(period);
    else/*from   w  w  w.j ava2s .  c  o  m*/
        throw new ServletException("Unknown period unit: " + periodUnits);
}

From source file:ch.eitchnet.android.mabea.MabeaParsing.java

License:Open Source License

public static Duration parseRemainingVacation(String value) throws MabeaParsingException {
    try {/*from   w w w.j a va2  s  . co m*/

        int posSpace = value.indexOf(' ');
        String daysS = value.substring(0, posSpace).replace(",", ".");
        double days = Double.parseDouble(daysS);

        Duration duration = Days.days((int) Math.floor(days)).toStandardDuration();
        if ((Math.floor(days)) <= (int) days) {
            duration = duration.plus(Hours.hours(12).toStandardDuration());
        }

        return duration;

    } catch (Exception e) {
        throw new MabeaParsingException("Remaining vacation can not be parsed from value " + value, e);
    }
}

From source file:ch.eitchnet.android.util.JodaHelper.java

License:Open Source License

public static String toDays(Duration duration) {
    Duration tmp = duration;/*  www  . j  a v  a 2  s. c  om*/
    double days = tmp.getStandardDays();
    if (days >= Integer.MAX_VALUE)
        throw new RuntimeException("Buffer overflow for number of days in duration: " + tmp);
    tmp = tmp.minus(Days.days((int) days).toStandardDuration());
    double hours = tmp.getStandardHours();
    if (hours >= Integer.MAX_VALUE)
        throw new RuntimeException("Buffer overflow for number of hours in duration: " + tmp);

    return Double.toString(days + (hours / 24));
}