Java Code Examples for java.time.Period#ofWeeks()

The following examples show how to use java.time.Period#ofWeeks() . 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: SimpleConfig.java    From waterdrop with Apache License 2.0 6 votes vote down vote up
private static Period periodOf(int n, ChronoUnit unit){
    if(unit.isTimeBased()){
        throw new DateTimeException(unit + " cannot be converted to a java.time.Period");
    }

    switch (unit){
        case DAYS:
            return Period.ofDays(n);
        case WEEKS:
            return Period.ofWeeks(n);
        case MONTHS:
            return Period.ofMonths(n);
        case YEARS:
            return Period.ofYears(n);
        default:
            throw new DateTimeException(unit + " cannot be converted to a java.time.Period");
    }
}
 
Example 2
Source File: ParserOfPeriod.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public final Period apply(String value) {
    try {
        if (getNullChecker().applyAsBoolean(value)) {
            return null;
        } else {
            final Matcher matcher = pattern.matcher(value);
            if (matcher.matches()) {
                final int digits = Integer.parseInt(matcher.group(1));
                final char code = matcher.group(2).toUpperCase().charAt(0);
                switch (code) {
                    case 'D':   return Period.ofDays(digits);
                    case 'W':   return Period.ofWeeks(digits);
                    case 'M':   return Period.ofMonths(digits);
                    case 'Y':   return Period.ofYears(digits);
                    default:    throw new IllegalArgumentException("Unsupported period type: " + code);
                }
            } else {
                throw new IllegalArgumentException("Cannot parse value into an Period: " + value + " pattern: " + pattern.pattern());
            }
        }
    } catch (Exception ex) {
        throw new FormatException("Failed to parse value into Period: " + value, ex);
    }
}
 
Example 3
Source File: DatedEvaluatorTest.java    From swblocks-decisiontree with Apache License 2.0 6 votes vote down vote up
@Test
public void singleRule() {
    final Builder<RuleSetBuilder, DecisionTreeRuleSet> ruleSetBuilder = ruleSetBuilder();
    final Instant now = Instant.now();
    final Period period = Period.ofWeeks(4);

    CommisionRuleSetSupplier.addRule(ruleSetBuilder, "*", "CME", "*", "*", "INDEX", now.minus(period),
            now.plus(period), 1L, "1.1");
    final DecisionTreeRuleSet ruleSet = ruleSetBuilder.build();
    final TreeNode node = constructTree(ruleSet);
    Assert.assertNotNull(node);

    checkMatch(inputC, now, node, 1L);

    checkNoMatch(inputC, now.plus(Period.ofWeeks(8)), node);
    checkNoMatch(inputC, now.minus(Period.ofWeeks(8)), node);

    checkMatch(inputC, now.plus(Period.ofDays(2)), node, 1L);

    checkNoMatch(inputA, now, node);
    checkNoMatch(inputB, now, node);
    checkNoMatch(inputD, now, node);
    checkNoMatch(inputE, now, node);
    checkNoMatch(inputF, now, node);
}
 
Example 4
Source File: Frequency.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains an instance backed by a period of weeks.
 *
 * @param weeks  the number of weeks
 * @return the periodic frequency
 * @throws IllegalArgumentException if weeks is negative or zero
 */
public static Frequency ofWeeks(int weeks) {
  switch (weeks) {
    case 1:
      return P1W;
    case 2:
      return P2W;
    case 4:
      return P4W;
    case 13:
      return P13W;
    case 26:
      return P26W;
    case 52:
      return P52W;
    default:
      return new Frequency(Period.ofWeeks(weeks), "P" + weeks + "W");
  }
}
 
Example 5
Source File: FrequencyTest.java    From Strata with Apache License 2.0 6 votes vote down vote up
public static Object[][] data_normalized() {
  return new Object[][] {
      {Period.ofDays(1), Period.ofDays(1)},
      {Period.ofDays(7), Period.ofDays(7)},
      {Period.ofDays(10), Period.ofDays(10)},
      {Period.ofWeeks(2), Period.ofDays(14)},
      {Period.ofMonths(1), Period.ofMonths(1)},
      {Period.ofMonths(2), Period.ofMonths(2)},
      {Period.ofMonths(12), Period.ofYears(1)},
      {Period.ofYears(1), Period.ofYears(1)},
      {Period.ofMonths(20), Period.of(1, 8, 0)},
      {Period.ofMonths(24), Period.ofYears(2)},
      {Period.ofYears(2), Period.ofYears(2)},
      {Period.ofMonths(30), Period.of(2, 6, 0)},
  };
}
 
Example 6
Source File: TenorTest.java    From Strata with Apache License 2.0 6 votes vote down vote up
public static Object[][] data_ofPeriod() {
  return new Object[][] {
      {Period.ofDays(1), Period.ofDays(1), "1D"},
      {Period.ofDays(7), Period.ofDays(7), "1W"},
      {Period.ofDays(10), Period.ofDays(10), "10D"},
      {Period.ofWeeks(2), Period.ofDays(14), "2W"},
      {Period.ofMonths(1), Period.ofMonths(1), "1M"},
      {Period.ofMonths(2), Period.ofMonths(2), "2M"},
      {Period.ofMonths(12), Period.ofMonths(12), "12M"},
      {Period.ofYears(1), Period.ofYears(1), "1Y"},
      {Period.ofMonths(20), Period.ofMonths(20), "20M"},
      {Period.ofMonths(24), Period.ofMonths(24), "24M"},
      {Period.ofYears(2), Period.ofYears(2), "2Y"},
      {Period.ofMonths(30), Period.ofMonths(30), "30M"},
      {Period.of(2, 6, 0), Period.of(2, 6, 0), "2Y6M"},
  };
}
 
Example 7
Source File: TenorTest.java    From Strata with Apache License 2.0 6 votes vote down vote up
public static Object[][] data_normalized() {
  return new Object[][] {
      {Period.ofDays(1), Period.ofDays(1)},
      {Period.ofDays(7), Period.ofDays(7)},
      {Period.ofDays(10), Period.ofDays(10)},
      {Period.ofWeeks(2), Period.ofDays(14)},
      {Period.ofMonths(1), Period.ofMonths(1)},
      {Period.ofMonths(2), Period.ofMonths(2)},
      {Period.ofMonths(12), Period.ofMonths(12)},
      {Period.ofYears(1), Period.ofMonths(12)},
      {Period.ofMonths(20), Period.of(1, 8, 0)},
      {Period.ofMonths(24), Period.ofYears(2)},
      {Period.ofYears(2), Period.ofYears(2)},
      {Period.ofMonths(30), Period.of(2, 6, 0)},
  };
}
 
Example 8
Source File: SimpleConfig.java    From PlayerVaults with GNU General Public License v3.0 6 votes vote down vote up
private static Period periodOf(int n, ChronoUnit unit){
    if(unit.isTimeBased()){
        throw new DateTimeException(unit + " cannot be converted to a java.time.Period");
    }

    switch (unit){
        case DAYS:
            return Period.ofDays(n);
        case WEEKS:
            return Period.ofWeeks(n);
        case MONTHS:
            return Period.ofMonths(n);
        case YEARS:
            return Period.ofYears(n);
        default:
            throw new DateTimeException(unit + " cannot be converted to a java.time.Period");
    }
}
 
Example 9
Source File: FHIRPathUtil.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public static TemporalAmount getTemporalAmount(FHIRPathQuantityValue quantityValue) {
    int value = quantityValue.value().intValue();
    String unit = quantityValue.unit();
    switch (unit) {
    case "year":
    case "years":
        return Period.ofYears(value);
    case "month":
    case "months":
        return Period.ofMonths(value);
    case "week":
    case "weeks":
        return Period.ofWeeks(value);
    case "day":
    case "days":
        return Period.ofDays(value);
    case "hour":
    case "hours":
        return Duration.ofHours(value);
    case "minute":
    case "minutes":
        return Duration.ofMinutes(value);
    case "second":
    case "seconds":
        return Duration.ofSeconds(value);
    case "millisecond":
    case "milliseconds":
        return Duration.ofMillis(value);
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 10
Source File: DateTest.java    From java-master with Apache License 2.0 5 votes vote down vote up
@Test
public void test2() {
    Period tenDays = Period.between(LocalDate.of(2014, 3, 8), LocalDate.of(2014, 3, 18));
    // Duration和Period类都提供了很多非常方便的工厂类,直接创建对应的实例:
    Duration threeMinutes = Duration.ofMinutes(3);
    threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
    tenDays = Period.ofDays(10);
    Period threeWeeks = Period.ofWeeks(3);
    Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
}
 
Example 11
Source File: FrequencyTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
public static Object[][] data_create() {
  return new Object[][] {
      {Frequency.ofDays(1), Period.ofDays(1), "P1D"},
      {Frequency.ofDays(2), Period.ofDays(2), "P2D"},
      {Frequency.ofDays(6), Period.ofDays(6), "P6D"},
      {Frequency.ofDays(7), Period.ofDays(7), "P1W"},
      {Frequency.ofDays(91), Period.ofDays(91), "P13W"},
      {Frequency.ofWeeks(1), Period.ofDays(7), "P1W"},
      {Frequency.ofWeeks(3), Period.ofDays(21), "P3W"},
      {Frequency.ofMonths(8), Period.ofMonths(8), "P8M"},
      {Frequency.ofMonths(12), Period.ofMonths(12), "P12M"},
      {Frequency.ofMonths(18), Period.ofMonths(18), "P18M"},
      {Frequency.ofMonths(24), Period.ofMonths(24), "P24M"},
      {Frequency.ofMonths(30), Period.ofMonths(30), "P30M"},
      {Frequency.ofYears(1), Period.ofYears(1), "P1Y"},
      {Frequency.ofYears(2), Period.ofYears(2), "P2Y"},
      {Frequency.of(Period.of(1, 2, 3)), Period.of(1, 2, 3), "P1Y2M3D"},
      {Frequency.P1D, Period.ofDays(1), "P1D"},
      {Frequency.P1W, Period.ofWeeks(1), "P1W"},
      {Frequency.P2W, Period.ofWeeks(2), "P2W"},
      {Frequency.P4W, Period.ofWeeks(4), "P4W"},
      {Frequency.P13W, Period.ofWeeks(13), "P13W"},
      {Frequency.P26W, Period.ofWeeks(26), "P26W"},
      {Frequency.P52W, Period.ofWeeks(52), "P52W"},
      {Frequency.P1M, Period.ofMonths(1), "P1M"},
      {Frequency.P2M, Period.ofMonths(2), "P2M"},
      {Frequency.P3M, Period.ofMonths(3), "P3M"},
      {Frequency.P4M, Period.ofMonths(4), "P4M"},
      {Frequency.P6M, Period.ofMonths(6), "P6M"},
      {Frequency.P12M, Period.ofMonths(12), "P12M"},
  };
}
 
Example 12
Source File: JavaPeriodUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenTestPeriod_thenOk() {

    LocalDate startDate = LocalDate.of(2015, 2, 15);
    LocalDate endDate = LocalDate.of(2017, 1, 21);

    Period period = Period.between(startDate, endDate);

    LOG.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays()));

    assertFalse(period.isNegative());
    assertEquals(56, period.plusDays(50)
        .getDays());
    assertEquals(9, period.minusMonths(2)
        .getMonths());

    Period fromUnits = Period.of(3, 10, 10);
    Period fromDays = Period.ofDays(50);
    Period fromMonths = Period.ofMonths(5);
    Period fromYears = Period.ofYears(10);
    Period fromWeeks = Period.ofWeeks(40);

    assertEquals(280, fromWeeks.getDays());

    Period fromCharYears = Period.parse("P2Y");
    assertEquals(2, fromCharYears.getYears());
    Period fromCharUnits = Period.parse("P2Y3M5D");
    assertEquals(5, fromCharUnits.getDays());
}
 
Example 13
Source File: Schedule.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Create a schedule builder where events are triggered every {@code weeks}
 *
 * @param weeks the number of weeks between events
 * @return a schedule builder generating events every {@code weeks}
 * @throws IllegalArgumentException if {@code weeks} is negative
 */
public static Builder everyWeeks(int weeks) {
  return new Builder(Period.ofWeeks(checkInterval(weeks)));
}
 
Example 14
Source File: Tenor.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains an instance backed by a period of weeks.
 *
 * @param weeks  the number of weeks
 * @return the tenor
 * @throws IllegalArgumentException if weeks is negative or zero
 */
public static Tenor ofWeeks(int weeks) {
  return new Tenor(Period.ofWeeks(weeks), weeks + "W");
}