Instant minusNanos() method in Java with Examples
minusNanos() method of Instant class subtracts nanoseconds value passed as parameter from this instant and return the result as an instant object. This returned Instant is immutable.
Syntax:
public Instant minusNanos(long nanosToSubtract)
Parameters: This method accepts one parameter nanosToSubtract which is nanoseconds to be subtracted.
Returns: This method returns Instant after subtraction of nanoseconds.
Exception: This method throws following exceptions:
- DateTimeException: if the result exceeds the maximum or minimum instant.
- ArithmeticException: if numeric overflow occurs.
Below programs illustrate the minusNanos() method:
Program 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
Program 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
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#minusNanos(long)
Recommended Posts:
- OffsetDateTime minusNanos() method in Java with examples
- LocalTime minusNanos() method in Java with Examples
- OffsetTime minusNanos() method in Java with examples
- ZonedDateTime minusNanos() method in Java with Examples
- Duration minusNanos(long) method in Java with Examples
- Instant from() method in Java with Examples
- Instant plus() method in Java with Examples
- Instant get() method in Java with Examples
- Instant with() Method in Java with Examples
- Instant now() Method in Java with Examples
- Instant until() Method in Java with Examples
- Instant equals() method in Java with Examples
- Instant minusMillis() method in Java with Examples
- Instant compareTo() method in Java with Examples
- Instant plusNanos() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



