Java SQL Timestamp setTime() function with examples Last Updated : 25 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The setTime() function is a part of Timestamp class of Java SQL.The function is used to set the time of the Timestamp object. The function takes time in milliseconds which represents the time in milliseconds after 1st January, 1970. Function Signature: public void setTime(long t) Syntax: ts1.setTime(l); Parameters: The function accepts a long value l as parameter which is to be set as the time. Return value: The function does not return any value. Exception: The function does not throw any exceptions. Below programs illustrates the use of setTime() function Example 1: Create a timestamp and use the setTime() to change the time of timestamp object. Java // Java program to demonstrate the // use of setTime() function import java.sql.*; public class solution { public static void main(String args[]) { // Create two timestamp objects Timestamp ts = new Timestamp(10000); // Display the timestamp object System.out.println("Timestamp time: " + ts.toString()); // Set the value of timestamp object // using setTime function ts.setTime(1000000000); // Display the new timestamp object System.out.println("New Timestamp time: " + ts.toString()); } } Output: Timestamp time: 1970-01-01 00:00:10.0 New Timestamp time: 1970-01-12 13:46:40.0 Example 2: Create a timestamp and use the setTime() to change the time of timestamp object by passing negative long value as parameter. Giving negative long value represents the time before 1st January 1970 Java // Java program to demonstrate the // use of setTime() function import java.sql.*; public class solution { public static void main(String args[]) { // Create two timestamp objects Timestamp ts = new Timestamp(10000); // Display the timestamp object System.out.println("Timestamp time: " + ts.toString()); // Set the value of timestamp object // using setTime function ts.setTime(-1000000000); // Display the new timestamp object System.out.println("New Timestamp time: " + ts.toString()); } } Output: Timestamp time: 1970-01-01 00:00:10.0 New Timestamp time: 1969-12-20 10:13:20.0 Reference: https://docs.oracle.com/javase/9/docs/api/java/sql/Timestamp.html#setTime-long- 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 Tutorial15+ 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