📌  相关文章
📜  Java中的 OffsetTime plusNanos() 方法及示例

📅  最后修改于: 2022-05-13 01:55:19.187000             🧑  作者: Mango

Java中的 OffsetTime plusNanos() 方法及示例

OffsetTime 类plusNanos()方法用于向此 OffsetTime 添加指定的纳秒值,并将结果作为 OffsetTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。

句法:

public OffsetTime plusNanos(long nanosecondsToAdd)

参数:此方法接受单个参数nanosecondsToAdd ,它是要添加的纳秒值,它可以是负值。

返回值:此方法基于此时间返回一个OffsetTime ,并添加纳秒。

下面的程序说明了 plusNanos() 方法:

方案一:

// Java program to demonstrate
// OffsetTime.plusNanos() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a OffsetTime object
        OffsetTime time
            = OffsetTime.parse("12:25:10+01:00");
  
        // print OffsetTime
        System.out.println("OffsetTime before add: "
                           + time);
  
        // add 34000000 nanoseconds using plusNanos()
        OffsetTime value = time.plusNanos(34000000);
  
        // print result
        System.out.println("OffsetTime after add: "
                           + value);
    }
}
输出:
OffsetTime before add: 12:25:10+01:00
OffsetTime after add: 12:25:10.034+01:00

方案二:

// Java program to demonstrate
// OffsetTime.plusNanos() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a OffsetTime object
        OffsetTime time
            = OffsetTime.parse("12:25:10+01:00");
  
        // print OffsetTime
        System.out.println("OffsetTime before add: "
                           + time);
  
        // add -971200000 nanoseconds  using plusNanos()
        OffsetTime value = time.plusNanos(-971200000);
  
        // print result
        System.out.println("OffsetTime after add: "
                           + value);
    }
}
输出:
OffsetTime before add: 12:25:10+01:00
OffsetTime after add: 12:25:09.028800+01:00

参考资料: https: Java/time/OffsetTime.html#plusNanos-long-