Instant plusNanos() method in Java with Examples
Last Updated :
28 Nov, 2018
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:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-10-28T19:34:50.63Z" );
Instant returnedValue
= instant.plusNanos( 840000000 );
System.out.println( "Returned Instant: "
+ returnedValue);
}
}
|
Output:
Returned Instant: 2018-10-28T19:34:51.470Z
Program 2:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant instant = Instant.now();
System.out.println( "Current instant: "
+ instant);
Instant returnedValue
= instant.plusNanos( 430000000 );
System.out.println( "Returned Instant: "
+ returnedValue);
}
}
|
Output:
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)
Share your thoughts in the comments
Please Login to comment...