The setNanos() function is a part of Timestamp class of Java SQL.The function is used to set the fractional part of the second’s value of the Timestamp Object. The function sets the object’s Nanos value to the given value.
public void setNanos(int t)
Parameters: The function accepts int value t as parameter which is the nanoseconds value to be set.
Return value: The function does not return any value.
Exception: The function throws IllegalArgumentException if the parameter passed is less than 0 or greater than 999999999
Below examples illustrate the use of setNanos() function
Example 1: Create a timestamp and use the setNanos() to set the fractional part of timestamp object.
Timestamp time : 1970-01-01 00:00:10.0 New Timestamp time : 1970-01-01 00:00:10.001
Example 2: Create a timestamp and use the setNanos() to set the fractional part of timestamp object and pass negative int value as parameter.
Timestamp time : 1970-01-01 00:00:10.0 nanos > 999999999 or < 0
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.