本文整理汇总了Java中java.time.Period类的典型用法代码示例。如果您正苦于以下问题:Java Period类的具体用法?Java Period怎么用?Java Period使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Period类属于java.time包,在下文中一共展示了Period类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRangeOfLocalDates
import java.time.Period; //导入依赖的package包/类
@Test(dataProvider = "localDateRanges")
public void testRangeOfLocalDates(LocalDate start, LocalDate end, Period step, boolean parallel) {
final Range<LocalDate> range = Range.of(start, end, step);
final Array<LocalDate> array = range.toArray(parallel);
final boolean ascend = start.isBefore(end);
final int expectedLength = (int)Math.ceil(Math.abs((double)ChronoUnit.DAYS.between(start, end)) / (double)step.getDays());
Assert.assertEquals(array.length(), expectedLength);
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_DATE);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
LocalDate expected = null;
for (int i=0; i<array.length(); ++i) {
final LocalDate actual = array.getValue(i);
expected = expected == null ? start : ascend ? expected.plus(step) : expected.minus(step);
Assert.assertEquals(actual, expected, "Value matches at " + i);
Assert.assertTrue(ascend ? actual.compareTo(start) >=0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + i);
}
}
示例2: testConstruction
import java.time.Period; //导入依赖的package包/类
@Test
public void testConstruction() {
final Instant start = Instant.now().minus(Period.ofWeeks(-1));
final Instant end = start.plus(Period.ofWeeks(5));
final InputDriver[] drivers = getInputDriverArray("Test1", "Test2", "Test3");
final Map<String, String> outputs = Collections.singletonMap("outputDriver", "result");
final DecisionTreeRule rule =
new DecisionTreeRule(new UUID(0, 1), new UUID(0, 2), drivers, outputs, start, end);
final RuleChange change = new RuleChange(Type.ORIGINAL, rule);
setBean(change);
this.injectedValues.put("type", Type.ORIGINAL);
this.injectedValues.put("rule", rule);
JblTestClassUtils.assertGetterCorrectForConstructorInjection(this.injectedValues, getBean());
}
示例3: test_resolveMinuteOfDay
import java.time.Period; //导入依赖的package包/类
@Test(dataProvider="resolveMinuteOfDay")
public void test_resolveMinuteOfDay(ResolverStyle style, long value, Integer expectedMinute, int expectedDays) {
String str = Long.toString(value);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(MINUTE_OF_DAY).toFormatter();
if (expectedMinute != null) {
TemporalAccessor accessor = f.withResolverStyle(style).parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), null);
assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.ofSecondOfDay(expectedMinute * 60));
assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ofDays(expectedDays));
} else {
try {
f.withResolverStyle(style).parse(str);
fail();
} catch (DateTimeParseException ex) {
// expected
}
}
}
示例4: testEquals
import java.time.Period; //导入依赖的package包/类
@Test
public void testEquals() {
final UUID id = new UUID(0, 1);
final Instant start = Instant.now().minus(Period.ofWeeks(-1));
final Instant end = start.plus(Period.ofWeeks(5));
final DateRange range = new DateRange(start, end);
final List<String> drivers = Arrays.asList("Test1", "Test2", "Test3");
final ValueGroup valueGroup = new ValueGroup(id, "TestValueGroup", drivers, range);
final ValueGroupChange change = new ValueGroupChange(Type.NEW, valueGroup);
assertTrue(change.equals(change));
assertFalse(change.equals(null));
assertFalse(change.equals(Boolean.TRUE));
ValueGroupChange other = new ValueGroupChange(Type.NEW, valueGroup);
assertTrue(change.equals(other));
final ValueGroup otherValueGroup = new ValueGroup(id, "VG:TestOtherValueGroup", drivers, range);
other = new ValueGroupChange(Type.NEW, otherValueGroup);
assertFalse(change.equals(other));
other = new ValueGroupChange(Type.ORIGINAL, otherValueGroup);
assertFalse(change.equals(other));
}
示例5: transform
import java.time.Period; //导入依赖的package包/类
@Override
public IDestinationRecord transform(ISourceRecord source) {
if(source == null){
return null;
}
int age = Period.between(source.date(), mAsAt).getYears();
return new IDestinationRecord() {
@Override
public String name() {
return source.name();
}
@Override
public int age() {
return age;
}
};
}
示例6: hashCodeCorrect
import java.time.Period; //导入依赖的package包/类
@Test
public void hashCodeCorrect() {
final Instant start = Instant.now().minus(Period.ofWeeks(-1));
final Instant end = start.plus(Period.ofWeeks(5));
final InputDriver[] drivers = getInputDriverArray("Test1", "Test2", "Test3");
final Map<String, String> outputs = Collections.singletonMap("outputDriver", "result");
final DecisionTreeRule rule =
new DecisionTreeRule(new UUID(0, 1), new UUID(0, 2), drivers, outputs, start, end);
final RuleChange ruleChange = new RuleChange(Type.ORIGINAL, rule);
final DecisionTreeRule otherRule =
new DecisionTreeRule(new UUID(0, 1), new UUID(0, 2), drivers, outputs, start, end);
final RuleChange otherRuleChange = new RuleChange(Type.ORIGINAL, otherRule);
assertEquals(ruleChange.hashCode(), otherRuleChange.hashCode());
}
示例7: PrinterOfObject
import java.time.Period; //导入依赖的package包/类
/**
* Constructor
* @param nullValue the null value supplier
*/
PrinterOfObject(Supplier<String> nullValue) {
super(FunctionStyle.OBJECT, nullValue);
this.printerMap.put(boolean.class, Printer.ofBoolean());
this.printerMap.put(Boolean.class, Printer.ofBoolean());
this.printerMap.put(int.class, Printer.ofInt());
this.printerMap.put(Integer.class, Printer.ofInt());
this.printerMap.put(long.class, Printer.ofLong());
this.printerMap.put(Long.class, Printer.ofLong());
this.printerMap.put(double.class, Printer.ofDouble("0.000###;-0.000###"));
this.printerMap.put(Double.class, Printer.ofDouble("0.000###;-0.000###"));
this.printerMap.put(LocalDate.class, Printer.ofLocalDate(DateTimeFormatter.ISO_LOCAL_DATE));
this.printerMap.put(LocalTime.class, Printer.ofLocalTime(DateTimeFormatter.ISO_LOCAL_TIME));
this.printerMap.put(LocalDateTime.class, Printer.ofLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
this.printerMap.put(ZonedDateTime.class, Printer.ofZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME));
this.printerMap.put(Period.class, Printer.ofPeriod());
this.printerMap.put(ZoneId.class, Printer.ofZoneId());
this.printerMap.put(TimeZone.class, Printer.ofTimeZone());
this.printerMap.put(java.util.Date.class, Printer.ofDate("yyyy-MM-dd"));
this.printerMap.put(java.sql.Date.class, Printer.ofDate("yyyy-MM-dd"));
}
示例8: initNextTime
import java.time.Period; //导入依赖的package包/类
private ZonedDateTime initNextTime(ZonedDateTime start, ZonedDateTime now, Period p, Duration d) {
// if the start time is in the future next will just be start
ZonedDateTime next = start;
// if the start time is in the past, increment until we find the next time to execute
// cannot call isComplete() here as it depends on nextTime
if(startTime.compareTo(now) <= 0 && !schedule.getRunOnce()) {
// TODO: Look to optimize. Consider a one-second timer, it would take too long
// For example if only a single unit, e.g. only minutes, then can optimize relative to start
// if there are more than one unit, then the loop may be best as it will be difficult
while(next.compareTo(now) <= 0) {
next = next.plus(p);
next = next.plus(d);
}
}
return next;
}
示例9: amendsEndDateOfFirstSegmentToFinishBeforeSecondSegmentStarts
import java.time.Period; //导入依赖的package包/类
@Test
public void amendsEndDateOfFirstSegmentToFinishBeforeSecondSegmentStarts() {
createBuilder(new UUID(0, 1), new DateRange(null, NOW.plus(Period.ofWeeks(3))));
final List<RuleChange> changes = this.builder.build();
assertThat(changes, hasSize(2));
final List<RuleChange> originals = getChangesByType(changes, Type.ORIGINAL);
assertThat(originals, hasSize(1));
assertRuleChange(originals.get(0), Type.ORIGINAL, new UUID(0, 1), new UUID(0, 2),
new String[]{"VOICE", "CME", "ED", "US", "INDEX"}, Collections.singletonMap("Rate", "1.1"),
NOW.plus(Period.ofWeeks(2)), NOW.plus(Period.ofWeeks(4)).minusMillis(1L));
final List<RuleChange> newChanges = getChangesByType(changes, Type.NEW);
assertThat(newChanges, hasSize(1));
assertRuleChange(newChanges.get(0), Type.NEW, null, new UUID(0, 2),
new String[]{"VOICE", "CME", "ED", "US", "INDEX"}, Collections.singletonMap("Rate", "1.1"),
NOW.plus(Period.ofWeeks(2)), NOW.plus(Period.ofWeeks(3)));
}
示例10: test_equals
import java.time.Period; //导入依赖的package包/类
public void test_equals() {
assertEquals(Period.of(1, 0, 0).equals(Period.ofYears(1)), true);
assertEquals(Period.of(0, 1, 0).equals(Period.ofMonths(1)), true);
assertEquals(Period.of(0, 0, 1).equals(Period.ofDays(1)), true);
assertEquals(Period.of(1, 2, 3).equals(Period.of(1, 2, 3)), true);
assertEquals(Period.ofYears(1).equals(Period.ofYears(1)), true);
assertEquals(Period.ofYears(1).equals(Period.ofYears(2)), false);
assertEquals(Period.ofMonths(1).equals(Period.ofMonths(1)), true);
assertEquals(Period.ofMonths(1).equals(Period.ofMonths(2)), false);
assertEquals(Period.ofDays(1).equals(Period.ofDays(1)), true);
assertEquals(Period.ofDays(1).equals(Period.ofDays(2)), false);
assertEquals(Period.of(1, 2, 3).equals(Period.of(0, 2, 3)), false);
assertEquals(Period.of(1, 2, 3).equals(Period.of(1, 0, 3)), false);
assertEquals(Period.of(1, 2, 3).equals(Period.of(1, 2, 0)), false);
}
示例11: assertOriginalSegmentsRemoved
import java.time.Period; //导入依赖的package包/类
private void assertOriginalSegmentsRemoved(final List<RuleChange> originals) {
assertRuleChange(originals.get(0), Type.ORIGINAL, new UUID(0, 1), new UUID(0, 2),
new String[]{"VOICE", GroupDriver.VG_PREFIX + GROUP_ID, "ED", "US", "INDEX"},
Collections.singletonMap("Rate1", "1.1 "),
NOW.plus(Period.ofWeeks(2)), NOW.plus(Period.ofWeeks(4)));
assertRuleChange(originals.get(1), Type.ORIGINAL, new UUID(0, 2), new UUID(0, 2),
new String[]{"EMAIL", GroupDriver.VG_PREFIX + GROUP_ID, "ED", "US", "INDEX"},
Collections.singletonMap("Rate1", "1.2"),
NOW.plus(Period.ofWeeks(4)), NOW.plus(Period.ofWeeks(6)));
assertRuleChange(originals.get(2), Type.ORIGINAL, new UUID(0, 3), new UUID(0, 2),
new String[]{"ELECTRONIC", GroupDriver.VG_PREFIX + GROUP_ID, "ED", "US", "INDEX"},
Collections.singletonMap("Rate1", "1.3"),
NOW.plus(Period.ofWeeks(6)), NOW.plus(Period.ofWeeks(8)));
}
示例12: testConstruction
import java.time.Period; //导入依赖的package包/类
@Test
public void testConstruction() {
final UUID id = new UUID(0, 1);
final Instant start = Instant.now().minus(Period.ofWeeks(-1));
final Instant end = start.plus(Period.ofWeeks(5));
final DateRange range = new DateRange(start, end);
final List<String> drivers = Arrays.asList("Test1", "Test2", "Test3");
final ValueGroup valueGroup = new ValueGroup(id, "TestValueGroup", drivers, range);
final ValueGroupChange change = new ValueGroupChange(Type.NEW, valueGroup);
setBean(change);
this.injectedValues.put("type", Type.NEW);
this.injectedValues.put("valueGroup", valueGroup);
JblTestClassUtils.assertGetterCorrectForConstructorInjection(this.injectedValues, getBean());
}
示例13: addNodeUpdatesDateRangeEnd
import java.time.Period; //导入依赖的package包/类
@Test
public void addNodeUpdatesDateRangeEnd() {
final TreeNode root = createDatedTreeNode("Root", Instant.MIN, Instant.MAX);
final Instant start = NOW.minus(Period.ofWeeks(5));
final Instant end = NOW.plus(Period.ofWeeks(5));
final TreeNode node = createDatedTreeNode("Test1", start, end);
root.addNode(node);
final TreeNode other = createDatedTreeNode("Test1", start, Instant.MAX);
root.addNode(other);
assertEquals(node, root.getExactNode(node).get());
final Range<Instant> range = new Range<>(start, Instant.MAX);
assertEquals(range, node.getDateRange());
}
示例14: cacheAccess
import java.time.Period; //导入依赖的package包/类
@Test
@Concurrent(count = 20)
@Repeating(repetition = 200)
public void cacheAccess() {
final Instant now = Instant.now();
final int start = (int) (Math.random() * 100);
final int end = start + (int) (Math.random() * 100);
final Range<Instant> dateRange = new Range<>(now.plus(Period.ofWeeks(start)), now.plus(Period.ofWeeks(end)));
final TreeNode node = NodeSupplier.createTreeNode(
new StringDriver(UUID.randomUUID().toString()), NodeSupplier.ROOT_NODE_LEVEL).get();
final Optional<TreeNode> treeNode = Optional.of(node);
this.cache.put(dateRange, treeNode);
for (final Range<Instant> dr : this.cache.keys()) {
this.cache.get(Optional.of(dr));
}
this.cache.get(Optional.of(dateRange));
}
示例15: test_resolveSecondOfDay
import java.time.Period; //导入依赖的package包/类
@Test(dataProvider="resolveSecondOfDay")
public void test_resolveSecondOfDay(ResolverStyle style, long value, Integer expectedSecond, int expectedDays) {
String str = Long.toString(value);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(SECOND_OF_DAY).toFormatter();
if (expectedSecond != null) {
TemporalAccessor accessor = f.withResolverStyle(style).parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), null);
assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.ofSecondOfDay(expectedSecond));
assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ofDays(expectedDays));
} else {
try {
f.withResolverStyle(style).parse(str);
fail();
} catch (DateTimeParseException ex) {
// expected
}
}
}