當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LocalTime plusNanos()用法及代碼示例


LocalTime類的plusNanos()方法用於向此LocalTime添加指定的毫微秒值,並將結果作為LocalTime對象返回。這一瞬間是一成不變的。計算大約在午夜結束。

用法:

public LocalTime plusNanos(long nanosecondsToAdd)

參數:此方法接受單個參數nanosecondsToAdd,該參數是要添加的納秒值,它可以是負值。


返回值:此方法基於此時間返回LocalTime,並添加了納秒。

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

示例1:

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

示例2:

// 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://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#plusNanos(long)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalTime plusNanos() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。