Java中的 LocalTime plusNanos() 方法及示例
LocalTime类的plusNanos()方法用于向此 LocalTime 添加指定的纳秒值,并将结果作为 LocalTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。
句法:
public LocalTime plusNanos(long nanosecondsToAdd)
参数:此方法接受单个参数nanosecondsToAdd ,它是要添加的纳秒值,它可以是负值。
返回值:此方法基于此时间返回一个LocalTime ,并添加纳秒。
下面的程序说明了 plusNanos() 方法:
方案一:
// Java program to demonstrate
// LocalTime.plusNanos() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("19:34:50.63");
// print LocalTime
System.out.println("LocalTime before add: "
+ time);
// add 34000000 nanoseconds using plusNanos()
LocalTime value = time.plusNanos(34000000);
// print result
System.out.println("LocalTime after add: "
+ value);
}
}
输出:
LocalTime before add: 19:34:50.630
LocalTime after add: 19:34:50.664
方案二:
// Java program to demonstrate
// LocalTime.plusNanos() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("01:00:01");
// print LocalTime
System.out.println("LocalTime before add: "
+ time);
// add -971200000 nanoseconds using plusNanos()
LocalTime value = time.plusNanos(-971200000);
// print result
System.out.println("LocalTime after add: "
+ value);
}
}
输出:
LocalTime before add: 01:00:01
LocalTime after add: 01:00:00.028800
参考资料: https: Java/time/LocalTime.html#plusNanos(long)