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-
相關用法
- Java Instant plusNanos()用法及代碼示例
- Java ZonedDateTime plusNanos()用法及代碼示例
- Java OffsetDateTime plusNanos()用法及代碼示例
- Java LocalDateTime plusNanos()用法及代碼示例
- Java LocalTime plusNanos()用法及代碼示例
- Java Duration plusNanos(long)用法及代碼示例
- Java OffsetTime now()用法及代碼示例
- Java OffsetTime from()用法及代碼示例
- Java OffsetTime until()用法及代碼示例
- Java OffsetTime get()用法及代碼示例
- Java OffsetTime with()用法及代碼示例
- Java OffsetTime plus()用法及代碼示例
- Java OffsetTime equals()用法及代碼示例
- Java OffsetTime format()用法及代碼示例
- Java OffsetTime getOffset()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 OffsetTime plusNanos() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。