Java SQL Timestamp getNanos() function with examples
The getNanos() function is a part of Timestamp class of Java SQL.The function is used to get the fractional part of the seconds value of the Timestamp Object. The function returns the object’s nanos value .
Function Signature:
public int getNanos()
Syntax:
ts1.getNanos();
Parameters: The function does not accept any parameter.
Return value: The function returns an integer value which is the object’s nanos value
Exception: The function does not throw any exception
Below examples illustrate the use of getNanos() function
Example 1: Create a timestamp and use the getNanos() to get the fractional part of timestamp object.
// Java program to demonstrate the // use of getNanos() 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 timestamp object's // seconds' fractional part System.out.println("New Timestamp time : " + ts.toString()); System.out.println(" Fractional Part :" + ts.getNanos()); } catch (IllegalArgumentException e) { // Display the error if any error has occured System.err.println(e.getMessage()); } } } |
Timestamp time : 1970-01-01 00:00:10.0 New Timestamp time : 1970-01-01 00:00:10.001 Fractional Part :1000000
Example 2: Create a timestamp and use the getNanos() to get the fractional part of timestamp object and dont set any fractional value of seconds
// Java program to demonstrate the // use of getNanos() 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()); // Display the timestamp object's // seconds' fractional part System.out.println("Fractional Part : " + ts.getNanos()); } catch (IllegalArgumentException e) { // Display the error if any error has occured System.err.println(e.getMessage()); } } } |
Timestamp time : 1970-01-01 00:00:10.0 Fractional Part : 0
Reference: https:// docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
Recommended Posts:
- Java SQL Timestamp after() function with examples
- Java SQL Timestamp before() function with examples
- Java SQL Timestamp setNanos() function with examples
- Java SQL Timestamp getTime() function with examples
- Java SQL Timestamp setTime() function with examples
- UUID timestamp() Method in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Function Interface in Java with Examples
- BlockingDeque offer() function in Java with Examples
- ZipFile entries() function in Java with examples
- Deflater setLevel() function in Java with examples
- Java ZipFile getInputStream() function 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.



