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

The following examples show how to use java.time.Duration#of() . 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: PoisonPlugin.java    From plugins with GNU General Public License v3.0 6 votes vote down vote up
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 File: TimeUtils.java    From micrometer with 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 File: LatencyConstraintTest.java    From onos with 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 File: TypeUtils.java    From incubator-heron with 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 5
Source File: LatencyConstraintTest.java    From onos with 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 6
Source File: Clock.java    From Enzo with 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 File: FuzzTester.java    From ShedLock with 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 8
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 9
Source File: TCKDuration.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKDuration.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 11
Source File: TCKDuration.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@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 12
Source File: TCKDuration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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 13
Source File: TCKDuration.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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 14
Source File: TCKDuration.java    From j2objc with 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
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
Example 16
Source File: TCKDuration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
Example 17
Source File: TCKDuration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 18
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
Example 19
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
Example 20
Source File: PauseStep.java    From TweetwallFX with MIT License 4 votes vote down vote up
private Duration getDuration() {
    return Duration.of(getAmount(), getUnit());
}