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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。