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


Java Instant minusNanos()用法及代碼示例


Instant類的minusNanos()方法從此瞬間減去作為參數傳遞的納秒值,並將結果作為即時對象返回。此返回的Instant是不可變的。

用法:

public Instant minusNanos(long nanosToSubtract)

參數:此方法接受一個參數nanosToSubtract,該參數要減去十億分之一秒。


返回值:減去納秒後,此方法返回Instant。

異常:此方法引發以下異常:

  • DateTimeException:如果結果超過最大或最小瞬間。
  • ArithmeticException:如果發生數字溢出。

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

示例1:

// Java program to demonstrate 
// Instant.minusNanos() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant 
            = Instant.parse("2018-12-30T19:34:50.63Z"); 
  
        // current Instant 
        System.out.println("Initailize instant: "
                           + instant); 
  
        // subtract 430000000 nanoseconds 
        // means .43 seconds from this instant 
        Instant returnedValue 
            = instant.minusNanos(430000000); 
  
        // print result 
        System.out.println("Returned Instant: "
                           + returnedValue); 
    } 
}
輸出:
Initailize instant: 2018-12-30T19:34:50.630Z
Returned Instant: 2018-12-30T19:34:50.200Z

示例2:

// Java program to demonstrate 
// Instant.minusNanos() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant = Instant.now(); 
  
        // current Instant 
        System.out.println("Current instant: "
                           + instant); 
  
        // subtract 540000000 nanoseconds 
        // means .564 seconds from this instant 
        Instant returnedValue 
            = instant.minusNanos(540000000); 
  
        // print result 
        System.out.println("Returned Instant: "
                           + returnedValue); 
    } 
}
輸出:
Current instant: 2018-11-27T06:43:58.495Z
Returned Instant: 2018-11-27T06:43:57.955Z

參考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#minusNanos(long)



相關用法


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