當前位置: 首頁>>代碼示例>>Java>>正文


Java Duration.of方法代碼示例

本文整理匯總了Java中java.time.Duration.of方法的典型用法代碼示例。如果您正苦於以下問題:Java Duration.of方法的具體用法?Java Duration.of怎麽用?Java Duration.of使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.time.Duration的用法示例。


在下文中一共展示了Duration.of方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: simpleParse

import java.time.Duration; //導入方法依賴的package包/類
public static Duration simpleParse(String time){
    String timeLower = 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);
}
 
開發者ID:micrometer-metrics,項目名稱:micrometer,代碼行數:18,代碼來源:TimeUtils.java

示例2: test_chronologyGetAvailablePerf

import java.time.Duration; //導入方法依賴的package包/類
@Test
public void test_chronologyGetAvailablePerf() {
    long start = System.nanoTime();
    Set<Chronology> chronos = Chronology.getAvailableChronologies();
    long end = System.nanoTime();
    Duration d = Duration.of(end - start, ChronoUnit.NANOS);
    System.out.printf(" Cold Duration of Chronology.getAvailableChronologies(): %s%n", d);

    start = System.nanoTime();
    chronos = Chronology.getAvailableChronologies();
    end = System.nanoTime();
    d = Duration.of(end - start, ChronoUnit.NANOS);
    System.out.printf(" Warm Duration of Chronology.getAvailableChronologies(): %s%n", d);

    start = System.nanoTime();
    HijrahChronology.INSTANCE.date(1434, 1, 1);
    end = System.nanoTime();
    d = Duration.of(end - start, ChronoUnit.NANOS);
    System.out.printf(" Warm Duration of HijrahDate.date(1434, 1, 1): %s%n", d);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:TestChronologyPerf.java

示例3: testLessThanLatency

import java.time.Duration; //導入方法依賴的package包/類
/**
 * Tests the path latency is less than the supplied constraint.
 */
@Test
public void testLessThanLatency() {
    sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS));

    assertThat(sut.validate(path, resourceContext), is(true));
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:10,代碼來源:LatencyConstraintTest.java

示例4: testMoreThanLatency

import java.time.Duration; //導入方法依賴的package包/類
/**
 * Tests the path latency is more than the supplied constraint.
 */
@Test
public void testMoreThanLatency() {
    sut = new LatencyConstraint(Duration.of(3, ChronoUnit.MICROS));

    assertThat(sut.validate(path, resourceContext), is(false));
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:10,代碼來源:LatencyConstraintTest.java

示例5: testCost

import java.time.Duration; //導入方法依賴的package包/類
/**
 * Tests the link latency is equal to "latency" annotated value.
 */
@Test
public void testCost() {
    sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS));

    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)));
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:11,代碼來源:LatencyConstraintTest.java

示例6: testEquality

import java.time.Duration; //導入方法依賴的package包/類
/**
 * Tests equality of the instances.
 */
@Test
public void testEquality() {
    LatencyConstraint c1 = new LatencyConstraint(Duration.of(1, ChronoUnit.SECONDS));
    LatencyConstraint c2 = new LatencyConstraint(Duration.of(1000, ChronoUnit.MILLIS));

    LatencyConstraint c3 = new LatencyConstraint(Duration.of(2, ChronoUnit.SECONDS));
    LatencyConstraint c4 = new LatencyConstraint(Duration.of(2000, ChronoUnit.MILLIS));

    new EqualsTester()
            .addEqualityGroup(c1, c2)
            .addEqualityGroup(c3, c4)
            .testEquals();
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:17,代碼來源:LatencyConstraintTest.java

示例7: truncateToMinutes

import java.time.Duration; //導入方法依賴的package包/類
private Duration truncateToMinutes(Duration duration) {
	return Duration.of(duration.toMinutes(), MINUTES);
}
 
開發者ID:mobitopp,項目名稱:connection-scan,代碼行數:4,代碼來源:RelativeTime.java

示例8: of

import java.time.Duration; //導入方法依賴的package包/類
public static RelativeTime of(long amount, ChronoUnit unit) {
	return new RelativeTime(Duration.of(amount, unit));
}
 
開發者ID:mobitopp,項目名稱:connection-scan,代碼行數:4,代碼來源:RelativeTime.java

示例9: parseDuration

import java.time.Duration; //導入方法依賴的package包/類
static Duration parseDuration(String input) {
  if (input.startsWith("P") || input.startsWith("-P") || input.startsWith("+P")) {
    return Duration.parse(input);
  }

  String[] parts = splitNumericAndChar(input);
  String numberString = parts[0];
  String originalUnitString = parts[1];
  String unitString = originalUnitString;

  if (numberString.length() == 0) {
    throw new IllegalArgumentException(String.format("No number in duration value '%s'", input));
  }

  if (unitString.length() > 2 && !unitString.endsWith("s")) {
    unitString = unitString + "s";
  }

  ChronoUnit units;
  switch (unitString) {
    case "ns":
      units = ChronoUnit.NANOS;
      break;
    case "us":
      units = ChronoUnit.MICROS;
      break;
    case "":
    case "ms":
      units = ChronoUnit.MILLIS;
      break;
    case "s":
      units = ChronoUnit.SECONDS;
      break;
    case "m":
      units = ChronoUnit.MINUTES;
      break;
    case "h":
      units = ChronoUnit.HOURS;
      break;
    case "d":
      units = ChronoUnit.DAYS;
      break;
    default:
      throw new IllegalArgumentException(
          String.format("Could not parse time unit '%s' (try ns, us, ms, s, m, h, d)", originalUnitString));
  }

  return Duration.of(Long.parseLong(numberString), units);
}
 
開發者ID:scalecube,項目名稱:config,代碼行數:50,代碼來源:DurationParser.java

示例10: factory_of_longTemporalUnit

import java.time.Duration; //導入方法依賴的package包/類
@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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:7,代碼來源:TCKDuration.java

示例11: factory_of_longTemporalUnit_outOfRange

import java.time.Duration; //導入方法依賴的package包/類
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
    Duration.of(amount, unit);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:5,代碼來源:TCKDuration.java

示例12: factory_of_longTemporalUnit_estimatedUnit

import java.time.Duration; //導入方法依賴的package包/類
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
    Duration.of(2, WEEKS);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:5,代碼來源:TCKDuration.java

示例13: factory_of_longTemporalUnit_null

import java.time.Duration; //導入方法依賴的package包/類
@Test(expectedExceptions=NullPointerException.class)
public void factory_of_longTemporalUnit_null() {
    Duration.of(1, (TemporalUnit) null);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:5,代碼來源:TCKDuration.java

示例14: TimeWait

import java.time.Duration; //導入方法依賴的package包/類
public TimeWait(final int amount, final TemporalUnit unit) {
    this(Duration.of(amount, unit));
}
 
開發者ID:fabzo,項目名稱:kraken,代碼行數:4,代碼來源:TimeWait.java

示例15: toDuration

import java.time.Duration; //導入方法依賴的package包/類
static Duration toDuration(long delay, TimeUnit timeUnit) {
    return Duration.of(delay, toChronoUnit(timeUnit));
}
 
開發者ID:vsilaev,項目名稱:tascalate-concurrent,代碼行數:4,代碼來源:Timeouts.java


注:本文中的java.time.Duration.of方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。