Instant类的plusNanos()方法将作为参数传递的纳秒值添加到此瞬时,并将结果作为瞬时对象返回。此返回的Instant是不可变的。
用法:
public Instant plusNanos(long nanosToAdd)
参数:此方法接受一个要添加的纳秒参数nanosToAdd。
返回值:此方法返回Instant,而不是null。
异常:此方法引发以下异常:
DateTimeException:如果结果超过最大或最小瞬间。
ArithmeticException:如果发生数字溢出。
以下示例程序旨在说明plusNanos()方法:
示例1:
// Java program to demonstrate
// Instant.plusNanos() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant
= Instant.parse("2018-10-28T19:34:50.63Z");
// addition of 840000000 nanoseconds
// means .84 seconds to this instant
Instant returnedValue
= instant.plusNanos(840000000);
// print result
System.out.println("Returned Instant: "
+ returnedValue);
}
}
输出:
Returned Instant: 2018-10-28T19:34:51.470Z
示例2:
// Java program to demonstrate
// Instant.plusNanos() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant = Instant.now();
// current Instant
System.out.println("Current instant: "
+ instant);
// addition of 430000000 nanoseconds
// means .43 seconds to this instant
Instant returnedValue
= instant.plusNanos(430000000);
// print result
System.out.println("Returned Instant: "
+ returnedValue);
}
}
输出:
Current instant: 2018-11-28T05:32:40.818Z
Returned Instant: 2018-11-28T05:32:41.248Z
参考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#plusNanos(long)