当前位置: 首页>>代码示例>>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;未经允许,请勿转载。