Java SQL Timestamp getNanos() function with examples Last Updated : 06 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 // 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 occurred System.err.println(e.getMessage()); } } } Output: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 // 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 occurred System.err.println(e.getMessage()); } } } Output: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 Comment More infoAdvertise with us A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-Sql package Java-TimeStamp Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like