本文整理汇总了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);
}
示例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);
}
示例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));
}
示例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));
}
示例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)));
}
示例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();
}
示例7: truncateToMinutes
import java.time.Duration; //导入方法依赖的package包/类
private Duration truncateToMinutes(Duration duration) {
return Duration.of(duration.toMinutes(), MINUTES);
}
示例8: of
import java.time.Duration; //导入方法依赖的package包/类
public static RelativeTime of(long amount, ChronoUnit unit) {
return new RelativeTime(Duration.of(amount, unit));
}
示例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);
}
示例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);
}
示例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);
}
示例12: factory_of_longTemporalUnit_estimatedUnit
import java.time.Duration; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
Duration.of(2, WEEKS);
}
示例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);
}
示例14: TimeWait
import java.time.Duration; //导入方法依赖的package包/类
public TimeWait(final int amount, final TemporalUnit unit) {
this(Duration.of(amount, unit));
}
示例15: toDuration
import java.time.Duration; //导入方法依赖的package包/类
static Duration toDuration(long delay, TimeUnit timeUnit) {
return Duration.of(delay, toChronoUnit(timeUnit));
}