当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java OffsetTime plusNanos()用法及代码示例


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

用法:

public OffsetTime plusNanos(long nanosecondsToAdd)

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


返回值:此方法基于此时间返回OffsetTime(添加了纳秒)。

以下示例程序旨在说明plusNanos()方法:

示例1:

// 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

示例2:

// 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://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#plusNanos-long-



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 OffsetTime plusNanos() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。