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

The following examples show how to use org.joda.time.Period#days() . 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: ConvertersTest.java    From gson-jodatime-serialisers with MIT License 5 votes vote down vote up
/**
 * Tests that the {@link Converters#registerAll} method registers the converters successfully.
 */
@Test
public void testRegisterAll()
{
  final Gson gson = Converters.registerAll(new GsonBuilder()).create();
  final Container original = new Container();
  //noinspection deprecation
  original.dm = new DateMidnight();
  original.dt = new DateTime();
  original.dtz = DateTimeZone.forID("UTC");
  original.d = Duration.standardMinutes(30L);
  original.ld = new LocalDate();
  original.ldt = new LocalDateTime();
  original.lt = new LocalTime();
  original.i = new Interval(DateTime.now().minusDays(14), DateTime.now().plusDays(2));
  original.p = Period.days(2);

  final Container reconstituted = gson.fromJson(gson.toJson(original), Container.class);

  assertThat(reconstituted.dm, is(original.dm));
  assertThat(reconstituted.dt.toString(), is(original.dt.toString()));  // work-around the loss of zone name and just worry about the offset
  assertThat(reconstituted.dtz, is(original.dtz));
  assertThat(reconstituted.d, is(original.d));
  assertThat(reconstituted.ld, is(original.ld));
  assertThat(reconstituted.ldt, is(original.ldt));
  assertThat(reconstituted.lt, is(original.lt));
  assertThat(reconstituted.i, is(original.i));
  assertThat(reconstituted.p, is(original.p));
}
 
Example 2
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 3
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_de_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(DE).parsePeriod("2 Tage"));
}
 
Example 4
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_pt_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(PT).parsePeriod("2 dias"));
}
 
Example 5
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_pt_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 dias", PeriodFormat.wordBased(PT).print(p));
}
 
Example 6
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_fr_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(FR).parsePeriod("2 jours"));
}
 
Example 7
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_fr_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 jours", PeriodFormat.wordBased(FR).print(p));
}
 
Example 8
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_es_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 d\u00EDas", PeriodFormat.wordBased(ES).print(p));
}
 
Example 9
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");
}
 
Example 10
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_nl_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(NL).parsePeriod("2 dagen"));
}
 
Example 11
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_de_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(DE).parsePeriod("2 Tage"));
}
 
Example 12
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_de_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 Tage", PeriodFormat.wordBased(DE).print(p));
}
 
Example 13
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_es_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(ES).parsePeriod("2 d\u00EDas"));
}
 
Example 14
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_es_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 d\u00EDas", PeriodFormat.wordBased(ES).print(p));
}
 
Example 15
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_pt_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(PT).parsePeriod("2 dias"));
}
 
Example 16
Source File: EstatioFakeDataService.java    From estatio with Apache License 2.0 4 votes vote down vote up
@Programmatic
public Period days(final int minDays, final int maxDays) {
    return Period.days(fakeDataService.ints().between(minDays, maxDays));
}
 
Example 17
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_fr_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 jours", PeriodFormat.wordBased(FR).print(p));
}
 
Example 18
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_wordBased_nl_parseOneField() {
    Period p = Period.days(2);
    assertEquals(p, PeriodFormat.wordBased(NL).parsePeriod("2 dagen"));
}
 
Example 19
Source File: TestPeriodFormat.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_getDefault_FormatOneField() {
    Period p = Period.days(2);
    assertEquals("2 days", PeriodFormat.getDefault().print(p));
}
 
Example 20
Source File: AnomalyDetectorWrapper.java    From incubator-pinot with Apache License 2.0 3 votes vote down vote up
/**
 * Speed up minute level detection.
 *
 * It will generate lots of small windows if the bucket size smaller than 15 minutes and detection window larger than 1 day.
 * This optimization is to change the bucket period, window size and window unit to 1 day.
 * Please note we need to change all the three parameters together since the detection window is:
 * [bucketPeriod_end - windowSize * windowUnit, bucketPeriod_end]
 *
 * It is possible to have bucketPeriod as 5 minutes but windowSize is 6 hours.
 */
private void speedUpMinuteLevelDetection() {
  if (bucketPeriod.toStandardDuration().getMillis() <= Period.minutes(15).toStandardDuration().getMillis()
      && endTime - startTime >= Period.days(1).toStandardDuration().getMillis()) {
    bucketPeriod = Period.days(1);
    windowSize = 1;
    windowUnit = TimeUnit.DAYS;
  }
}