Java SQL Timestamp setTime() function with examples
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 funtion 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 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()); } } |
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 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()); } } |
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-
Recommended Posts:
- Java SQL Timestamp before() function with examples
- Java SQL Timestamp after() function with examples
- Java SQL Timestamp getNanos() function with examples
- Java SQL Timestamp setNanos() function with examples
- Java SQL Timestamp getTime() function with examples
- Calendar setTime() Method in Java with Examples
- Date setTime() method in Java with Examples
- UUID timestamp() Method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Function Interface in Java with Examples
- Deflater needsInput() function in Java with examples
- Deflater getTotalIn() function in Java 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.



