Java Timestamp setNanos() Method

The setNanos() method of Timestamp class sets a nanos value for the specified integer value.

Syntax

Parameters

Here the parameter 'n' represents the fractional seconds component.

Throws

The setNanos() method of Timestamp class throws:

IllegalArgumentException- if the given argument is less than 0 and greater than 999999999

Example 1

Output:

Timestamp : 2018-09-01 09:01:15.0
Timestamp after setting nanos : 2018-09-01 09:01:15.000054647

Example 2

Output:

Exception in thread "main" Timestamp : 2018-09-01 09:01:15.0
java.lang.IllegalArgumentException: nanos > 999999999 or < 0
	at java.sql.Timestamp.setNanos(Timestamp.java:389)
	at JavaTimestampSetNanosExample2.main(JavaTimestampSetNanosExample2.java:7)

An runtime exception IllegalArgumentException is thrown if the given argument is less than 0 and greater than 999999999