Example usage for org.joda.time Period getHours

List of usage examples for org.joda.time Period getHours

Introduction

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

Prototype

public int getHours() 

Source Link

Document

Gets the hours field part of the period.

Usage

From source file:org.eclim.plugin.core.command.history.HistoryListCommand.java

License:Open Source License

private String delta(long time) {
    // FIXME: a formatter can probably do this.
    Period period = new Period(time, System.currentTimeMillis());
    ArrayList<String> parts = new ArrayList<String>();

    int years = period.getYears();
    if (years > 0) {
        parts.add(years + " year" + (years == 1 ? "" : "s"));
    }//from   w  w  w . j ava 2 s .c  o  m

    int months = period.getMonths();
    if (months > 0) {
        parts.add(months + " month" + (months == 1 ? "" : "s"));
    }

    int weeks = period.getWeeks();
    if (weeks > 0) {
        parts.add(weeks + " week" + (weeks == 1 ? "" : "s"));
    }

    int days = period.getDays();
    if (days > 0) {
        parts.add(days + " day" + (days == 1 ? "" : "s"));
    }

    int hours = period.getHours();
    if (hours > 0) {
        parts.add(hours + " hour" + (hours == 1 ? "" : "s"));
    }

    int minutes = period.getMinutes();
    if (minutes > 0) {
        parts.add(minutes + " minute" + (minutes == 1 ? "" : "s"));
    }

    int seconds = period.getSeconds();
    if (seconds > 0) {
        parts.add(seconds + " second" + (seconds == 1 ? "" : "s"));
    }

    if (parts.size() == 0) {
        int millis = period.getMillis();
        if (millis > 0) {
            parts.add(millis + " millis");
        }
    }

    return StringUtils.join(parts.toArray(), ' ') + " ago";
}

From source file:org.gdg.frisbee.android.utils.Utils.java

License:Apache License

public static String toHumanTimePeriod(Context ctx, DateTime start, DateTime end) {
    String result;/*from w w w  .  ja  v a2s.  c  om*/
    Resources res = ctx.getResources();
    Period p = new Period(start, end);

    if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0 && p.getHours() == 0
            && p.getMinutes() == 0) {
        result = res.getQuantityString(R.plurals.seconds_ago, p.getSeconds(), p.getSeconds());
    } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0
            && p.getHours() == 0) {
        result = res.getQuantityString(R.plurals.minutes_ago, p.getMinutes(), p.getMinutes());
    } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0) {
        result = res.getQuantityString(R.plurals.hours_ago, p.getHours(), p.getHours());
    } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0) {
        result = res.getQuantityString(R.plurals.days_ago, p.getDays(), p.getDays());
    } else {
        result = start.toLocalDateTime()
                .toString(DateTimeFormat.patternForStyle("M-", res.getConfiguration().locale));
    }
    return result;
}

From source file:org.gephi.desktop.timeline.DateTick.java

License:Open Source License

public static DateTick create(double min, double max, int width) {

    DateTime minDate = new DateTime((long) min);
    DateTime maxDate = new DateTime((long) max);

    Period period = new Period(minDate, maxDate, PeriodType.yearMonthDayTime());
    ;//from w  w w.ja  v  a  2  s  .c om
    int years = period.getYears();
    int months = period.getMonths();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();

    //Top type
    DateTimeFieldType topType;
    if (years > 0) {
        topType = DateTimeFieldType.year();
    } else if (months > 0) {
        topType = DateTimeFieldType.monthOfYear();
    } else if (days > 0) {
        topType = DateTimeFieldType.dayOfMonth();
    } else if (hours > 0) {
        topType = DateTimeFieldType.hourOfDay();
    } else if (minutes > 0) {
        topType = DateTimeFieldType.minuteOfHour();
    } else if (seconds > 0) {
        topType = DateTimeFieldType.secondOfMinute();
    } else {
        topType = DateTimeFieldType.millisOfSecond();
    }

    //Bottom type
    if (topType != DateTimeFieldType.millisOfSecond()) {
        DateTimeFieldType bottomType;
        if (topType.equals(DateTimeFieldType.year())) {
            bottomType = DateTimeFieldType.monthOfYear();
        } else if (topType.equals(DateTimeFieldType.monthOfYear())) {
            bottomType = DateTimeFieldType.dayOfMonth();
        } else if (topType.equals(DateTimeFieldType.dayOfMonth())) {
            bottomType = DateTimeFieldType.hourOfDay();
        } else if (topType.equals(DateTimeFieldType.hourOfDay())) {
            bottomType = DateTimeFieldType.minuteOfHour();
        } else if (topType.equals(DateTimeFieldType.minuteOfHour())) {
            bottomType = DateTimeFieldType.secondOfMinute();
        } else {
            bottomType = DateTimeFieldType.millisOfSecond();
        }

        //Number of ticks
        Period p = new Period(minDate, maxDate,
                PeriodType.forFields(new DurationFieldType[] { bottomType.getDurationType() }));
        int intervals = p.get(bottomType.getDurationType());
        if (intervals > 0) {
            int intervalSize = width / intervals;
            if (intervalSize >= MIN_PIXELS) {
                return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType, bottomType });
            }
        }
    }

    return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType });
}

From source file:org.graylog2.indexer.rotation.strategies.TimeBasedRotationStrategy.java

License:Open Source License

/**
 * Determines the starting point ("anchor") for a period.
 *
 * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time.
 * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute
 * the first rotation was started./*from ww  w  . j a  v a2  s .co m*/
 *
 * This "snapping" is done accordingly with the other parts of a period.
 *
 * For highly irregular periods (those that do not have a small zero component)
 *
 * @param period the rotation period
 * @return the anchor DateTime to calculate rotation periods from
 */
protected static DateTime determineRotationPeriodAnchor(@Nullable DateTime lastAnchor, Period period) {
    final Period normalized = period.normalizedStandard();
    int years = normalized.getYears();
    int months = normalized.getMonths();
    int weeks = normalized.getWeeks();
    int days = normalized.getDays();
    int hours = normalized.getHours();
    int minutes = normalized.getMinutes();
    int seconds = normalized.getSeconds();

    if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        throw new IllegalArgumentException("Invalid rotation period specified");
    }

    // find the largest non-zero stride in the period. that's our anchor type. statement order matters here!
    DateTimeFieldType largestStrideType = null;
    if (seconds > 0)
        largestStrideType = secondOfMinute();
    if (minutes > 0)
        largestStrideType = minuteOfHour();
    if (hours > 0)
        largestStrideType = hourOfDay();
    if (days > 0)
        largestStrideType = dayOfMonth();
    if (weeks > 0)
        largestStrideType = weekOfWeekyear();
    if (months > 0)
        largestStrideType = monthOfYear();
    if (years > 0)
        largestStrideType = year();
    if (largestStrideType == null) {
        throw new IllegalArgumentException("Could not determine rotation stride length.");
    }

    final DateTime anchorTime = MoreObjects.firstNonNull(lastAnchor, Tools.nowUTC());

    final DateTimeField field = largestStrideType.getField(anchorTime.getChronology());
    // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836
    int periodValue = normalized.get(largestStrideType.getDurationType());
    final long fieldValue = field.roundFloor(anchorTime.getMillis());

    final int fieldValueInUnit = field.get(fieldValue);
    if (periodValue == 0) {
        // https://github.com/Graylog2/graylog2-server/issues/836
        log.warn(
                "Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!");
        periodValue = 1;
    }
    final long difference = (fieldValueInUnit % periodValue);
    final long newValue = field.add(fieldValue, -1 * difference);
    return new DateTime(newValue, DateTimeZone.UTC);
}

From source file:org.graylog2.indexer.rotation.TimeBasedRotationStrategy.java

License:Open Source License

/**
 * Determines the starting point ("anchor") for a period.
 *
 * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time.
 * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute
 * the first rotation was started./* ww  w.  j ava 2  s .  com*/
 *
 * This "snapping" is done accordingly with the other parts of a period.
 *
 * For highly irregular periods (those that do not have a small zero component)
 *
 * @param period the rotation period
 * @return the anchor DateTime to calculate rotation periods from
 */
protected static DateTime determineRotationPeriodAnchor(Period period) {
    final Period normalized = period.normalizedStandard();
    int years = normalized.getYears();
    int months = normalized.getMonths();
    int weeks = normalized.getWeeks();
    int days = normalized.getDays();
    int hours = normalized.getHours();
    int minutes = normalized.getMinutes();
    int seconds = normalized.getSeconds();

    if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        throw new IllegalArgumentException("Invalid rotation period specified");
    }

    // find the largest non-zero stride in the period. that's our anchor type. statement order matters here!
    DateTimeFieldType largestStrideType = null;
    if (seconds > 0)
        largestStrideType = secondOfMinute();
    if (minutes > 0)
        largestStrideType = minuteOfHour();
    if (hours > 0)
        largestStrideType = hourOfDay();
    if (days > 0)
        largestStrideType = dayOfMonth();
    if (weeks > 0)
        largestStrideType = weekOfWeekyear();
    if (months > 0)
        largestStrideType = monthOfYear();
    if (years > 0)
        largestStrideType = year();
    if (largestStrideType == null) {
        throw new IllegalArgumentException("Could not determine rotation stride length.");
    }

    final DateTime now = Tools.iso8601();

    final DateTimeField field = largestStrideType.getField(now.getChronology());
    // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836
    int periodValue = normalized.get(largestStrideType.getDurationType());
    final long fieldValue = field.roundFloor(now.getMillis());

    final int fieldValueInUnit = field.get(fieldValue);
    if (periodValue == 0) {
        // https://github.com/Graylog2/graylog2-server/issues/836
        log.warn(
                "Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!");
        periodValue = 1;
    }
    final long difference = (fieldValueInUnit % periodValue);
    final long newValue = field.add(fieldValue, -1 * difference);
    return new DateTime(newValue, DateTimeZone.UTC);
}

From source file:org.hawkular.metrics.core.impl.DateTimeService.java

License:Apache License

public DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*from  www . j a v a2 s .c  o  m*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.hawkular.metrics.datetime.DateTimeService.java

License:Apache License

public static DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*w ww.  j  a v  a  2 s. c  o m*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.hawkular.metrics.tasks.api.AbstractTrigger.java

License:Apache License

protected DateTime getExecutionTime(long time, Duration duration) {
    DateTime dt = new DateTime(time);
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*from ww w.  j a  v a 2s  .co  m*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.jbpm.process.core.timer.BusinessCalendarImpl.java

License:Apache License

protected String adoptISOFormat(String timeExpression) {

    try {//  w  w w .  j av a  2  s .  c o  m
        Period p = null;
        if (DateTimeUtils.isPeriod(timeExpression)) {
            p = ISOPeriodFormat.standard().parsePeriod(timeExpression);
        } else {
            DateTime dt = ISODateTimeFormat.dateTimeParser().parseDateTime(timeExpression);
            Duration duration = new Duration(System.currentTimeMillis(), dt.getMillis());

            p = duration.toPeriod();
        }
        int days = p.getDays();
        int hours = p.getHours();
        int minutes = p.getMinutes();
        int seconds = p.getSeconds();
        int milis = p.getMillis();

        StringBuffer time = new StringBuffer();
        if (days > 0) {
            time.append(days + "d");
        }
        if (hours > 0) {
            time.append(hours + "h");
        }
        if (minutes > 0) {
            time.append(minutes + "m");
        }
        if (seconds > 0) {
            time.append(seconds + "s");
        }
        if (milis > 0) {
            time.append(milis + "ms");
        }

        return time.toString();
    } catch (Exception e) {
        return timeExpression;
    }
}

From source file:org.jevis.application.unit.SampleRateNode.java

License:Open Source License

private void buildNode(Period period) {
    //        System.out.println("new SampleRateNode: " + period.toString());
    //        final Period newPeriod = period;

    sliderMonth.setMin(0);/*from  www.  ja va2 s  . com*/
    sliderMonth.setMax(12);
    sliderMonth.setValue(period.getMonths());
    sliderMonth.setShowTickLabels(true);
    sliderMonth.setShowTickMarks(true);
    sliderMonth.setMajorTickUnit(6);
    //        sliderMonth.setMinorTickCount(5);
    sliderMonth.setBlockIncrement(1);

    sliderWeek.setMin(0);
    sliderWeek.setMax(5);
    sliderWeek.setValue(period.getWeeks());
    sliderWeek.setShowTickLabels(true);
    sliderWeek.setShowTickMarks(true);
    sliderWeek.setMajorTickUnit(1);
    //        sliderWeek.setMinorTickCount(5);
    sliderWeek.setBlockIncrement(1);

    sliderHours.setMin(0);
    sliderHours.setMax(180);
    sliderHours.setValue(period.getHours());
    sliderHours.setShowTickLabels(true);
    sliderHours.setShowTickMarks(true);
    sliderHours.setMajorTickUnit(15);
    //        sliderHours.setMinorTickCount(5);
    sliderHours.setBlockIncrement(1);

    sliderMinutes.setMin(0);
    sliderMinutes.setMax(60);
    sliderMinutes.setValue(period.getMinutes());
    sliderMinutes.setShowTickLabels(true);
    sliderMinutes.setShowTickMarks(true);
    sliderMinutes.setMajorTickUnit(15);
    //        sliderMonth.setMinorTickCount(5);
    sliderMinutes.setBlockIncrement(1);

    sliderSecounds.setMin(0);
    sliderSecounds.setMax(60);
    sliderSecounds.setValue(period.getSeconds());
    sliderSecounds.setShowTickLabels(true);
    sliderSecounds.setShowTickMarks(true);
    sliderSecounds.setMajorTickUnit(15);
    //        sliderMonth.setMinorTickCount(5);
    sliderSecounds.setBlockIncrement(1);

    final Label monthlabel = new Label("Months:");
    final Label weekslabel = new Label("Weeks:");
    final Label hourslabel = new Label("Hours:");
    final Label minuteslabel = new Label("Minutes:");
    final Label secoundslabel = new Label("Secounds:");
    final Label periodLabel = new Label("Sample Rate:");
    final TextField sampleRate = new TextField();
    sampleRate.setText(period.toString());

    //        Label enableLabel = new Label("Has fix sample rate:");
    final CheckBox enable = new CheckBox("Set fixed sample rate");

    setHgap(5);
    setVgap(5);
    setPadding(new Insets(10, 10, 10, 10));

    int i = 0;

    add(enable, 0, i, 2, 1);
    add(monthlabel, 0, ++i);
    add(weekslabel, 0, ++i);
    add(hourslabel, 0, ++i);
    add(minuteslabel, 0, ++i);
    add(secoundslabel, 0, ++i);
    add(periodLabel, 0, ++i);

    i = 0;
    //        add(enable, 1, i);
    add(sliderMonth, 1, ++i);
    add(sliderWeek, 1, ++i);
    add(sliderHours, 1, ++i);
    add(sliderMinutes, 1, ++i);
    add(sliderSecounds, 1, ++i);
    add(sampleRate, 1, ++i);

    enable.selectedProperty().addListener(new ChangeListener<Boolean>() {

        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            //                System.out.println("actioN!!!!!!!!");
            sliderMonth.setDisable(!enable.isSelected());
            sliderWeek.setDisable(!enable.isSelected());
            sliderHours.setDisable(!enable.isSelected());
            sliderMinutes.setDisable(!enable.isSelected());
            sliderSecounds.setDisable(!enable.isSelected());
            sampleRate.setDisable(!enable.isSelected());
            monthlabel.setDisable(!enable.isSelected());
            weekslabel.setDisable(!enable.isSelected());
            hourslabel.setDisable(!enable.isSelected());
            minuteslabel.setDisable(!enable.isSelected());
            secoundslabel.setDisable(!enable.isSelected());
            periodLabel.setDisable(!enable.isSelected());
        }
    });

    if (period.equals(Period.ZERO)) {
        enable.setSelected(false);
    } else {
        enable.setSelected(true);
    }

    ChangeListener<Number> sliderChanged = new ChangeListener<Number>() {

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            setPeriod(sampleRate);
        }
    };

    sliderMinutes.valueProperty().addListener(sliderChanged);
    sliderSecounds.valueProperty().addListener(sliderChanged);
    sliderMonth.valueProperty().addListener(sliderChanged);
    sliderWeek.valueProperty().addListener(sliderChanged);
    sliderHours.valueProperty().addListener(sliderChanged);

    setPeriod(sampleRate);

}