Example usage for org.joda.time Period getMonths

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

Introduction

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

Prototype

public int getMonths() 

Source Link

Document

Gets the months field part of the period.

Usage

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./*from w  w  w  . j  a v  a  2s. c om*/
 *
 * 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 ww  w. j  a  v a  2  s  . co  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());
    }//from ww  w. ja v a2  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  2 s.  c o m
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

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 w w  w .j av  a 2 s.  co m
    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);

}

From source file:org.jw.service.entity.Contact.java

@Transient
public String getAge() {
    if (this.birthdate == null)
        return "<No Birthdate>";

    LocalDate dob = LocalDate.fromDateFields(birthdate);
    LocalDate now = LocalDate.now();

    Period period = new Period(dob, now, PeriodType.yearMonthDay());

    return String.format("%d years & %d months", period.getYears(), period.getMonths());
}

From source file:org.kalypso.commons.time.PeriodUtils.java

License:Open Source License

/**
 * @return {@link Integer#MAX_VALUE} if the amount could not be determined.
 *///from  w ww  .j av  a2 s  .  c om
public static int findCalendarAmount(final Period period) {
    final int fieldCount = countNonZeroFields(period);
    if (fieldCount > 1)
        throw new IllegalArgumentException(
                "Unable to find calendar amount for periods with more than one field: " + period); //$NON-NLS-1$

    if (period.getDays() != 0)
        return period.getDays();

    if (period.getHours() != 0)
        return period.getHours();

    if (period.getMillis() != 0)
        return period.getMillis();

    if (period.getMinutes() != 0)
        return period.getMinutes();

    if (period.getMonths() != 0)
        return period.getMonths();

    if (period.getSeconds() != 0)
        return period.getSeconds();

    if (period.getWeeks() != 0)
        return period.getWeeks();

    if (period.getYears() != 0)
        return period.getYears();

    return Integer.MAX_VALUE;
}

From source file:org.kalypso.commons.time.PeriodUtils.java

License:Open Source License

public static FIELD findCalendarField(final Period period) {
    final int fieldCount = countNonZeroFields(period);

    if (fieldCount > 1)
        throw new IllegalArgumentException(
                "Unable to find calendar field for periods with more than one field: " + period); //$NON-NLS-1$

    if (period.getDays() != 0)
        return FIELD.DAY_OF_MONTH;

    if (period.getHours() != 0)
        return FIELD.HOUR_OF_DAY;

    if (period.getMillis() != 0)
        return FIELD.MILLISECOND;

    if (period.getMinutes() != 0)
        return FIELD.MINUTE;

    if (period.getMonths() != 0)
        return FIELD.MONTH;

    if (period.getSeconds() != 0)
        return FIELD.SECOND;

    if (period.getWeeks() != 0)
        return FIELD.WEEK_OF_YEAR;

    if (period.getYears() != 0)
        return FIELD.YEAR;

    return null;//from   w ww  . j av a 2 s  .com
}

From source file:org.kalypso.ogc.sensor.metadata.MetadataHelper.java

License:Open Source License

public static void setTimestep(final MetadataList mdl, final Period timestep) {
    final int[] values = timestep.getValues();
    int fieldCount = 0;
    for (final int value : values) {
        if (value != 0)
            fieldCount++;//ww w . j av a  2 s  .c  om
    }

    if (fieldCount > 1)
        throw new IllegalArgumentException(Messages.getString("MetadataHelper_2") + timestep); //$NON-NLS-1$

    int amount = -1;
    int calendarField = -1;

    if (timestep.getDays() != 0) {
        amount = timestep.getDays();
        calendarField = Calendar.DAY_OF_MONTH;
    } else if (timestep.getHours() != 0) {
        amount = timestep.getHours();
        calendarField = Calendar.HOUR_OF_DAY;
    } else if (timestep.getMillis() != 0) {
        amount = timestep.getMillis();
        calendarField = Calendar.MILLISECOND;
    } else if (timestep.getMinutes() != 0) {
        amount = timestep.getMinutes();
        calendarField = Calendar.MINUTE;
    } else if (timestep.getMonths() != 0) {
        amount = timestep.getMonths();
        calendarField = Calendar.MONTH;
    } else if (timestep.getSeconds() != 0) {
        amount = timestep.getSeconds();
        calendarField = Calendar.SECOND;
    } else if (timestep.getWeeks() != 0) {
        amount = timestep.getWeeks();
        calendarField = Calendar.WEEK_OF_YEAR;
    } else if (timestep.getYears() != 0) {
        amount = timestep.getYears();
        calendarField = Calendar.YEAR;
    }

    if (amount == -1)
        throw new IllegalArgumentException(Messages.getString("MetadataHelper_3")); //$NON-NLS-1$

    setTimestep(mdl, calendarField, amount);

    return;
}

From source file:org.openmrs.module.chaiui.ChaiUiUtils.java

License:Open Source License

/**
 * Formats a person's age//from w ww  . ja va2 s  . c  o m
 * @param person the person
 * @return the string value
 */
public String formatPersonAge(Person person) {
    String prefix = BooleanUtils.isTrue(person.isBirthdateEstimated()) ? "~" : "";
    int ageYears = person.getAge();

    if (ageYears < 1) {
        Period p = new Period(person.getBirthdate().getTime(), System.currentTimeMillis(),
                PeriodType.yearMonthDay());
        return prefix + p.getMonths() + " month(s), " + p.getDays() + " day(s)";
    } else {
        return prefix + ageYears + " year(s)";
    }
}