Java SQL Timestamp setNanos() function with examples

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.

Function Signature:

public void setNanos(int t)

Syntax:



ts1.setNanos(l);

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of setNanos() function
  
import java.sql.*;
  
public class solution {
    public static void main(String args[])
    {
  
        try {
  
            // Create two timestamp objects
            Timestamp ts = new Timestamp(10000);
  
            // Display the timestamp object
            System.out.println("Timestamp time : "
                               + ts.toString());
  
            // Set the value of the fractional part
            // of timestamp object
            // using setNanos function
            ts.setNanos(1000000);
  
            // Display the new timestamp object
            System.out.println("New Timestamp time : "
                               + ts.toString());
        }
  
        catch (IllegalArgumentException e) {
  
            // Display the error if any error has occured
            System.err.println(e.getMessage());
        }
    }
}

chevron_right


Output:

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 negetive int value as parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of setNanos() function
  
import java.sql.*;
  
public class solution {
    public static void main(String args[])
    {
  
        try {
  
            // Create two timestamp objects
            Timestamp ts = new Timestamp(10000);
  
            // Display the timestamp object
            System.out.println("Timestamp time : "
                               + ts.toString());
  
            // Set the value of the fractional part
            // of timestamp object
            // using setNanos function
            ts.setNanos(-1000000);
  
            // Display the new timestamp object
            System.out.println("New Timestamp time : "
                               + ts.toString());
        }
  
        catch (IllegalArgumentException e) {
  
            // Display the error if any error has occured
            System.out.println(e.getMessage());
        }
    }
}

chevron_right


Output:

Timestamp time : 1970-01-01 00:00:10.0
nanos > 999999999 or < 0

Reference: https:// docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

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.