The plusNanos() method of Instant class adds nanoseconds value passed as parameter to this instant and return the result as an instant object. This returned Instant is immutable.
Syntax:
public Instant plusNanos(long nanosToAdd)
Parameters: This method accepts one parameter nanosToAdd which is nanoseconds to be added.
Returns: This method returns Instant, not null.
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 plusNanos() method:
Program 1:
// Java program to demonstrate // Instant.plusNanos() method import java.time.*; public class GFG { public static void main(String[] args) { // create a Instant object Instant instant = Instant.parse("2018-10-28T19:34:50.63Z"); // addition of 840000000 nanoseconds // means .84 seconds to this instant Instant returnedValue = instant.plusNanos(840000000); // print result System.out.println("Returned Instant: " + returnedValue); } } |
Returned Instant: 2018-10-28T19:34:51.470Z
Program 2:
// Java program to demonstrate // Instant.plusNanos() 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); // addition of 430000000 nanoseconds // means .43 seconds to this instant Instant returnedValue = instant.plusNanos(430000000); // print result System.out.println("Returned Instant: " + returnedValue); } } |
Current instant: 2018-11-28T05:32:40.818Z Returned Instant: 2018-11-28T05:32:41.248Z
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#plusNanos(long)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Duration plusNanos(long) method in Java with Examples
- LocalDateTime plusNanos() method in Java with Examples
- LocalTime plusNanos() method in Java with Examples
- ZonedDateTime plusNanos() method in Java with Examples
- OffsetDateTime plusNanos() method in Java with examples
- OffsetTime plusNanos() method in Java with Examples
- Instant isSupported() method in Java with Examples
- Java 8 Clock instant() method with Examples
- Instant equals() method in Java with Examples
- Instant compareTo() method in Java with Examples
- Instant atZone() method in Java with Examples
- Instant atOffset() method in Java with Examples
- Instant getEpochSecond() method in Java with Examples
- Instant getLong() method in Java with Examples
- Instant getNano() method in Java with Examples
- Instant hashCode() method in Java with Examples
- Instant toEpochMilli() method in Java with Examples
- Instant isAfter() method in Java with Examples
- Instant isBefore() method in Java with Examples
- Instant from() 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.

