Java Code Examples for org.joda.time.Period#years()

The following examples show how to use org.joda.time.Period#years() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestISOPeriodFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFormatStandard_negative() {
    Period p = new Period(-1, -2, -3, -4, -5, -6, -7, -8);
    assertEquals("P-1Y-2M-3W-4DT-5H-6M-7.008S", ISOPeriodFormat.standard().print(p));
    
    p = Period.years(-54);
    assertEquals("P-54Y", ISOPeriodFormat.standard().print(p));
    
    p = Period.seconds(4).withMillis(-8);
    assertEquals("PT3.992S", ISOPeriodFormat.standard().print(p));
    
    p = Period.seconds(-4).withMillis(8);
    assertEquals("PT-3.992S", ISOPeriodFormat.standard().print(p));
    
    p = Period.seconds(-23);
    assertEquals("PT-23S", ISOPeriodFormat.standard().print(p));
    
    p = Period.millis(-8);
    assertEquals("PT-0.008S", ISOPeriodFormat.standard().print(p));
}
 
Example 2
Source File: TestISOPeriodFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFormatStandard_negative() {
    Period p = new Period(-1, -2, -3, -4, -5, -6, -7, -8);
    assertEquals("P-1Y-2M-3W-4DT-5H-6M-7.008S", ISOPeriodFormat.standard().print(p));
    
    p = Period.years(-54);
    assertEquals("P-54Y", ISOPeriodFormat.standard().print(p));
    
    p = Period.seconds(4).withMillis(-8);
    assertEquals("PT3.992S", ISOPeriodFormat.standard().print(p));
    
    p = Period.seconds(-4).withMillis(8);
    assertEquals("PT-3.992S", ISOPeriodFormat.standard().print(p));
    
    p = Period.seconds(-23);
    assertEquals("PT-23S", ISOPeriodFormat.standard().print(p));
    
    p = Period.millis(-8);
    assertEquals("PT-0.008S", ISOPeriodFormat.standard().print(p));
}
 
Example 3
Source File: CalendarDuration.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert a time udunit string
 * 
 * @param value number of units
 * @param udunit msec, sec, minute, hour, hr, day, week, month, year (plural form ok)
 * @return joda Period
 */
static org.joda.time.Period convertToPeriod(int value, String udunit) {
  if (udunit.endsWith("s"))
    udunit = udunit.substring(0, udunit.length() - 1);

  switch (udunit) {
    case "msec":
      return Period.millis(value);
    case "sec":
      return Period.seconds(value);
    case "minute":
      return Period.minutes(value);
    case "hour":
    case "hr":
      return Period.hours(value);
    case "day":
      return Period.days(value);
    case "week":
      return Period.weeks(value);
    case "month":
      return Period.months(value);
    case "year":
      return Period.years(value);
  }

  throw new IllegalArgumentException("cant convert " + udunit + " to Joda Period");
}
 
Example 4
Source File: EstatioFakeDataService.java    From estatio with Apache License 2.0 4 votes vote down vote up
@Programmatic
public Period years(final int minYears, final int maxYears) {
    return Period.years(fakeDataService.ints().between(minYears, maxYears));
}
 
Example 5
Source File: IntervalParser.java    From crate with Apache License 2.0 4 votes vote down vote up
static Period roundToPrecision(Period period, Precision start, Precision end) {
    if (start == null && end == null) {
        return period;
    }
    if (start == Precision.YEAR) {
        if (end == null) {
            return Period.years(period.getYears());
        }
        if (end == Precision.MONTH) {
            return Period.years(period.getYears()).withMonths(period.getMonths());
        }
    }
    if (start == Precision.MONTH && end == null) {
        return Period.years(period.getYears()).withMonths(period.getMonths());
    }
    if (start == Precision.DAY) {
        if (end == null) {
            return period.withHours(0).withMinutes(0).withSeconds(0).withMillis(0);
        }
        if (end == Precision.HOUR) {
            return period.withMinutes(0).withSeconds(0).withMillis(0);
        }
        if (end == Precision.MINUTE) {
            return period.withSeconds(0).withMillis(0);
        }
        if (end == Precision.SECOND) {
            return period.withMillis(0);
        }
    }
    if (start == Precision.HOUR) {
        if (end == null) {
            return period.withMinutes(0).withSeconds(0).withMillis(0);
        }
        if (end == Precision.MINUTE) {
            return period.withSeconds(0).withMillis(0);
        }
        if (end == Precision.SECOND) {
            return period.withMillis(0);
        }
    }
    if (start == Precision.MINUTE) {
        if (end == null) {
            return period.withSeconds(0).withMillis(0);
        }
        if (end == Precision.SECOND) {
            return period.withMillis(0);
        }
    }
    if (start == Precision.SECOND && end == null) {
        return period.withMillis(0);
    }
    throw new IllegalArgumentException("Invalid start and end combination");
}
 
Example 6
Source File: NumericalIntervalParser.java    From crate with Apache License 2.0 4 votes vote down vote up
private static Period roundToPrecision(int value,
                                       int millis,
                                       @Nullable IntervalParser.Precision start,
                                       @Nullable IntervalParser.Precision end) {
    if (start == null && end == null) {
        return buildSecondsWithMillisPeriod(value, millis);
    }
    if (start == IntervalParser.Precision.YEAR) {
        if (end == null) {
            return Period.years(value);
        }
        if (end == IntervalParser.Precision.MONTH) {
            return Period.months(value);
        }
    }
    if (start == IntervalParser.Precision.MONTH && end == null) {
        return Period.months(value);
    }
    if (start == IntervalParser.Precision.DAY) {
        if (end == null) {
            return Period.days(value);
        }
        if (end == IntervalParser.Precision.HOUR) {
            return Period.hours(value);
        }
        if (end == IntervalParser.Precision.MINUTE) {
            return Period.minutes(value);
        }
        if (end == IntervalParser.Precision.SECOND) {
            return buildSecondsWithMillisPeriod(value, millis);
        }
    }
    if (start == IntervalParser.Precision.HOUR) {
        if (end == null) {
            return Period.hours(value);
        }
        if (end == IntervalParser.Precision.MINUTE) {
            return Period.minutes(value);
        }
        if (end == IntervalParser.Precision.SECOND) {
            return buildSecondsWithMillisPeriod(value, millis);
        }
    }
    if (start == IntervalParser.Precision.MINUTE) {
        if (end == null) {
            return Period.minutes(value);
        }
        if (end == IntervalParser.Precision.SECOND) {
            return Period.seconds(value);
        }
    }
    if (start == IntervalParser.Precision.SECOND && end == null) {
        return buildSecondsWithMillisPeriod(value, millis);
    }
    throw new IllegalArgumentException("Invalid start and end combination");
}