Example usage for org.joda.time Period getWeeks

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

Introduction

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

Prototype

public int getWeeks() 

Source Link

Document

Gets the weeks field part of the period.

Usage

From source file:net.longfalcon.view.DateView.java

License:Open Source License

public Period roundPeriod(Period period) {
    int fieldCount = 0;
    int years = period.getYears();
    int months = period.getMonths();
    int weeks = period.getWeeks();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();
    if (years > 0) {
        fieldCount++;//from   w  w w. jav a2s  .com
    }
    if (months > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(years, months, 0, 0, 0, 0, 0, 0);
    }

    if (weeks > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, months, weeks, 0, 0, 0, 0, 0);
    }

    if (days > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, weeks, days, 0, 0, 0, 0);
    }

    if (hours > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, 0, days, hours, 0, 0, 0);
    }

    if (minutes > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, 0, 0, hours, minutes, 0, 0);
    }

    return new Period(0, 0, 0, 0, 0, minutes, seconds, 0);
}

From source file:nz.co.gregs.dbvolution.databases.definitions.DBDefinition.java

License:Apache License

/**
 * Creates a string representation of a DateRepeat from the Period
 *
 * @param interval the interval to be transformed into a DateRepeat.
 * @return a DateRpeat as an SQL string/*w  w  w.jav a2  s  .c  om*/
 */
public String transformPeriodIntoDateRepeat(Period interval) {
    StringBuilder str = new StringBuilder();
    str.append("'").append(DateRepeatExpression.INTERVAL_PREFIX);
    str.append(interval.getYears()).append(DateRepeatExpression.YEAR_SUFFIX);
    str.append(interval.getMonths()).append(DateRepeatExpression.MONTH_SUFFIX);
    str.append(interval.getDays() + (interval.getWeeks() * 7)).append(DateRepeatExpression.DAY_SUFFIX);
    str.append(interval.getHours()).append(DateRepeatExpression.HOUR_SUFFIX);
    str.append(interval.getMinutes()).append(DateRepeatExpression.MINUTE_SUFFIX);
    str.append(interval.getSeconds()).append(DateRepeatExpression.SECOND_SUFFIX);
    str.append("'");
    return str.toString();
}

From source file:nz.co.gregs.dbvolution.internal.datatypes.DateRepeatImpl.java

License:Apache License

/**
 *
 * @param interval// ww  w.  ja v  a2 s.c om
 * @return the DateRepeat equivalent of the Period value
 */
public static String getDateRepeatString(Period interval) {
    if (interval == null) {
        return null;
    }
    int years = interval.getYears();
    int months = interval.getMonths();
    int days = interval.getDays() + interval.getWeeks() * 7;
    int hours = interval.getHours();
    int minutes = interval.getMinutes();

    int millis = interval.getMillis();
    double seconds = interval.getSeconds() + (millis / 1000.0);
    String intervalString = "P" + years + "Y" + months + "M" + days + "D" + hours + "h" + minutes + "n"
            + seconds + "s";
    return intervalString;
}

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