Java Code Examples for java.time.Duration#of()

The following examples show how to use java.time.Duration#of() . These examples are extracted from open source projects. 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
String createTooltip()
{
	Duration timeLeft;
	if (nextPoisonTick.isBefore(Instant.now()) && !envenomed)
	{
		timeLeft = Duration.of(POISON_TICK_MILLIS * (lastValue - 1), ChronoUnit.MILLIS);
	}
	else
	{
		timeLeft = Duration.between(Instant.now(), nextPoisonTick).plusMillis(POISON_TICK_MILLIS * (lastValue - 1));
	}

	String line1 = MessageFormat.format("Next {0} damage: {1}</br>Time until damage: {2}",
		envenomed ? "venom" : "poison", ColorUtil.wrapWithColorTag(String.valueOf(lastDamage), Color.RED),
		getFormattedTime(Duration.between(Instant.now(), nextPoisonTick)));
	String line2 = envenomed ? "" : MessageFormat.format("</br>Time until cure: {0}", getFormattedTime(timeLeft));

	return line1 + line2;
}
 
Example 2
Source Project: micrometer   File: TimeUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param time A time string ending in human readable suffixes like 'ns', 'ms', 's'.
 * @return A duration
 * @deprecated Use {@link DurationValidator#validate(String, String)} instead since 1.5.0.
 */
@Deprecated
public static Duration simpleParse(String time) {
    String timeLower = PARSE_PATTERN.matcher(time.toLowerCase()).replaceAll("");
    if (timeLower.endsWith("ns")) {
        return Duration.ofNanos(Long.parseLong(timeLower.substring(0, timeLower.length() - 2)));
    } else if (timeLower.endsWith("ms")) {
        return Duration.ofMillis(Long.parseLong(timeLower.substring(0, timeLower.length() - 2)));
    } else if (timeLower.endsWith("s")) {
        return Duration.ofSeconds(Long.parseLong(timeLower.substring(0, timeLower.length() - 1)));
    } else if (timeLower.endsWith("m")) {
        return Duration.ofMinutes(Long.parseLong(timeLower.substring(0, timeLower.length() - 1)));
    } else if (timeLower.endsWith("h")) {
        return Duration.ofHours(Long.parseLong(timeLower.substring(0, timeLower.length() - 1)));
    } else if (timeLower.endsWith("d")) {
        return Duration.of(Long.parseLong(timeLower.substring(0, timeLower.length() - 1)), ChronoUnit.DAYS);
    }
    throw new DateTimeParseException("Unable to parse " + time + " into duration", timeLower, 0);
}
 
Example 3
Source Project: onos   File: LatencyConstraintTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the path latency is more than the supplied constraint.
 */
@Test
public void testMoreThanLatency() {
    sut = new LatencyConstraint(Duration.of(3, ChronoUnit.NANOS));

    assertThat(sut.validate(path, resourceContext), is(false));
}
 
Example 4
Source Project: java-master   File: DateTest.java    License: 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 5
Source Project: ShedLock   File: FuzzTester.java    License: Apache License 2.0 5 votes vote down vote up
public LockConfiguration getLockConfiguration() {
    return new LockConfiguration(
        lockName,
        Duration.of(5, ChronoUnit.MINUTES),
        Duration.of(5, ChronoUnit.MILLIS)
    );
}
 
Example 6
Source Project: Enzo   File: Clock.java    License: Apache License 2.0 5 votes vote down vote up
public Clock(final LocalDateTime DATE_TIME) {
    getStyleClass().add("clock");
    _text                 = "";
    _discreteSecond       = false;
    _secondPointerVisible = true;
    _nightMode            = false;
    _design               = Design.IOS6;
    _highlightVisible     = true;
    _dateTime             = DATE_TIME;
    _offset               = Duration.of(0, ChronoUnit.MINUTES);
    _running              = false;
    _autoNightMode        = false;
}
 
Example 7
Source Project: onos   File: LatencyConstraintTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the link latency is equal to "latency" annotated value.
 */
@Test
public void testCost() {
    sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS));

    assertThat(sut.cost(link1, resourceContext), is(closeTo(Double.parseDouble(LATENCY1), 1.0e-6)));
    assertThat(sut.cost(link2, resourceContext), is(closeTo(Double.parseDouble(LATENCY2), 1.0e-6)));
}
 
Example 8
Source Project: incubator-heron   File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Duration getDuration(Object o, TemporalUnit unit) {
  if (o != null && o instanceof Duration) {
    return (Duration) o;
  }
  try {
    return Duration.of(getLong(o), unit);
  } catch (IllegalArgumentException e) {
    throw new IllegalArgumentException("Don't know how to convert " + o + " to Duration", e);
  }
}
 
Example 9
@Test(dataProvider="OfTemporalUnit")
public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
    Duration t = Duration.of(amount, unit);
    assertEquals(t.getSeconds(), expectedSeconds);
    assertEquals(t.getNano(), expectedNanoOfSecond);
}
 
Example 10
@Test(dataProvider="OfTemporalUnit")
public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
    Duration t = Duration.of(amount, unit);
    assertEquals(t.getSeconds(), expectedSeconds);
    assertEquals(t.getNano(), expectedNanoOfSecond);
}
 
Example 11
@Test(dataProvider="OfTemporalUnit")
public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
    Duration t = Duration.of(amount, unit);
    assertEquals(t.getSeconds(), expectedSeconds);
    assertEquals(t.getNano(), expectedNanoOfSecond);
}
 
Example 12
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 13
@Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class)
public void test_bad_getUnit(long amount, TemporalUnit unit) {
    Duration t = Duration.of(amount, unit);
    t.get(unit);
}
 
Example 14
Source Project: j2objc   File: TCKDuration.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 15
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
Example 16
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
Example 17
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 18
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
Example 19
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 20
Source Project: TweetwallFX   File: PauseStep.java    License: MIT License 4 votes vote down vote up
private Duration getDuration() {
    return Duration.of(getAmount(), getUnit());
}