Example usage for org.joda.time Period withFieldAdded

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

Introduction

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

Prototype

public Period withFieldAdded(DurationFieldType field, int value) 

Source Link

Document

Creates a new Period instance with the valueToAdd added to the specified field.

Usage

From source file:com.jbirdvegas.mgerrit.search.AgeSearch.java

License:Apache License

/**
 * Parses a string into a Period object according to the replacers
 *  mapping. This allows for duplicate fields (e.g. seconds being
 *  declared twice as in "2s 3 sec") with the duplicate fields being
 *  added together (the above example would be the same as "5 seconds").
 * The order of the fields is not important.
 *
 * @param dateOffset The parameter without the operator. If the operator
 *                   is passed in it will be ignored
 * @return A period corresponding to the parsed input string
 */// ww w. j a v a  2 s.c o m
private Period toPeriod(final String dateOffset) {
    String regexp = "(\\d+) *([a-zA-z]+)";
    Period period = new Period();

    if (dateOffset == null || dateOffset.isEmpty())
        return period;

    Pattern pattern = Pattern.compile(regexp);
    Matcher matcher = pattern.matcher(dateOffset);
    while (matcher.find()) {
        String svalue = matcher.toMatchResult().group(1);
        DurationFieldType fieldType = replacers.get(matcher.toMatchResult().group(2));
        if (fieldType != null) {
            // Note that both these methods do not modify their objects
            period = period.withFieldAdded(fieldType, Integer.parseInt(svalue));
        }
    }

    return period;
}

From source file:com.jbirdvegas.mgerrit.search.AgeSearch.java

License:Apache License

/**
 * Adds adjustment to the shortest set time range in period. E.g.
 *  period("5 days 3 hours", 1) -> "5 days 4 hours". This will fall
 *  back to adjusting years if no field in the period is set.
 * @param period The period to be adjusted
 * @param adjustment The adjustment. Note that positive values will result
 *                   in larger periods and an earlier time
 * @return The adjusted period/*from  w  ww. j  a  va 2  s.c o m*/
 */
private Period adjust(final Period period, int adjustment) {
    if (adjustment == 0)
        return period;

    // Order is VERY important here
    LinkedHashMap<Integer, DurationFieldType> map = new LinkedHashMap<>();
    map.put(period.getSeconds(), DurationFieldType.seconds());
    map.put(period.getMinutes(), DurationFieldType.minutes());
    map.put(period.getHours(), DurationFieldType.hours());
    map.put(period.getDays(), DurationFieldType.days());
    map.put(period.getWeeks(), DurationFieldType.weeks());
    map.put(period.getMonths(), DurationFieldType.months());
    map.put(period.getYears(), DurationFieldType.years());

    for (Map.Entry<Integer, DurationFieldType> entry : map.entrySet()) {
        if (entry.getKey() > 0) {
            return period.withFieldAdded(entry.getValue(), adjustment);
        }
    }
    // Fall back to modifying years
    return period.withFieldAdded(DurationFieldType.years(), adjustment);
}

From source file:com.metinkale.prayerapp.vakit.times.Times.java

License:Apache License

public String getLeft(int next, boolean showsecs) {
    LocalDateTime date = getTimeCal(null, next);
    Period period = new Period(LocalDateTime.now(), date, PeriodType.dayTime());

    if (showsecs) {
        return Utils.toArabicNrs(PERIOD_FORMATTER_HMS.print(period));
    } else if (Prefs.isDefaultWidgetMinuteType()) {
        return Utils.toArabicNrs(PERIOD_FORMATTER_HM.print(period));
    } else {//www.  j  a va2 s  . c  o  m
        period = period.withFieldAdded(DurationFieldType.minutes(), 1);
        return Utils.toArabicNrs(PERIOD_FORMATTER_HM.print(period));
    }

}