Example usage for org.joda.time Period normalizedStandard

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

Introduction

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

Prototype

public Period normalizedStandard() 

Source Link

Document

Normalizes this period using standard rules, assuming a 12 month year, 7 day week, 24 hour day, 60 minute hour and 60 second minute.

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   ww  w . java  2  s. 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.kalypso.ui.rrm.internal.calccase.LinearSumCatchmentModelInfo.java

License:Open Source License

@Override
public Period getTimestep() {
    Integer timestep = m_generator.getTimestep();
    if (timestep == null)
        timestep = m_control.getMinutesOfTimestep();

    final int timestepMinutes = timestep.intValue();
    final Period minutes = Period.minutes(timestepMinutes);

    return minutes.normalizedStandard();
}

From source file:org.n52.sos.ds.hibernate.SosCacheFeederDAO.java

License:Open Source License

private void logCacheLoadTime(long startTime) {
    Period cacheLoadPeriod = new Period(startTime, System.currentTimeMillis());
    LOGGER.info("Cache load finished in {} ({} seconds)",
            PeriodFormat.getDefault().print(cacheLoadPeriod.normalizedStandard()),
            cacheLoadPeriod.toStandardSeconds());
}

From source file:org.yamj.common.tools.DateTimeTools.java

License:Open Source License

/**
 * Format the duration in milliseconds in the given format
 *
 * @param milliseconds//from w  w w.ja v  a  2 s .c  o  m
 * @param format
 * @return
 */
public static String formatDuration(long milliseconds, PeriodFormatter format) {
    Period period = new Period(milliseconds, PeriodType.time());
    period = period.normalizedStandard();
    return format.print(period);
}

From source file:us.repasky.microblog.domain.Post.java

License:Apache License

protected static String getAge(final Date fromDate, final Date createdDate) {
    DateTime now = new DateTime(fromDate);
    DateTime created = new DateTime(createdDate);

    Period period = new Period(created, now);
    String age = "";

    int weeksAgo = period.getWeeks();
    if (weeksAgo == 0) {
        age = PERIOD_FORMATTER.print(period.normalizedStandard());
    } else {/* w w  w. j av a 2 s.c  o m*/
        age = "a long while ago";
    }

    return age;
}