java.time包中的Duration Class的withNanos(long)方法用于获取此持续时间的不可变副本,并将指定的纳秒量作为参数传递。
用法:
public Duration withNanos(long amountOfNanos)
参数:此方法接受参数纳秒数的数量OfNanos。
返回值:此方法返回作为参数传递的纳秒持续时间。
异常:如果纳秒无效,则此方法引发DateTimeException。
以下示例说明了Duration.withNanos()方法:
范例1:
// Java code to illustrate withNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Get the amount of nano-seconds
int amountOfNanos = 30000;
// Duration using parse() method
Duration duration
= Duration.parse("P2DT3H4M");
// Get the duration in nano-seconds
// using withNanos() method
System.out.println(duration
.withNanos(amountOfNanos));
}
}
输出:
PT51H4M0.00003S
范例2:
// Java code to illustrate withNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Get the amount of nano-seconds
int amountOfNanos = 100000;
// Duration using ofHours() method
Duration duration = Duration.ofHours(5);
// Get the duration in nano-seconds
// using withNanos() method
System.out.println(duration
.withNanos(amountOfNanos));
}
}
输出:
PT5H0.0001S
参考: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#withNanos-int-
相关用法
- Java Duration equals(Duration)用法及代码示例
- Java Duration compareTo(Duration)用法及代码示例
- Java Duration dividedBy(Duration)用法及代码示例
- Java Duration minus(Duration)用法及代码示例
- Java Duration plus(Duration)用法及代码示例
- Java Duration abs()用法及代码示例
- Java Duration getUnits()用法及代码示例
- Java Duration toMinutesPart()用法及代码示例
- Java Duration get(TemporalUnit)用法及代码示例
- Java Duration toNanos()用法及代码示例
- Java Duration toSecondsPart()用法及代码示例
- Java Duration toMillis()用法及代码示例
- Java Duration isNegative()用法及代码示例
- Java Duration toHours()用法及代码示例
- Java Duration toMinutes()用法及代码示例
注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Duration withNanos(long) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。