Saturday, October 11, 2025
HomeLanguagesJavaJava SQL Timestamp getTime() function with examples

Java SQL Timestamp getTime() function with examples

The getTime() function is a part of Timestamp class of Java SQL.The function is used to get the time of the Timestamp object. The function returns time in milliseconds which represents the time in milliseconds after 1st January 1970.

Function Signature:

public long getTime()

Syntax:

ts1.getTime();

Parameters: The function does not require any parameter.

Return value: The function returns long value representing time in milliseconds.

Exception: The function does not throw any exceptions.

Below examples illustrate the use of getTime() function

Example 1: Create a timestamp and use the getTime() to get the time of timestamp object.




// Java program to demonstrate the
// use of getTime() function
  
import java.sql.*;
  
class GFG {
    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());
        System.out.println("Time in milliseconds : "
                           + ts.getTime());
    }
}


Output:

Timestamp time : 1970-01-01 00:00:10.0
Time in milliseconds : 10000

Example 2: Create a timestamp and use the getTime() to get the time of timestamp object and set the time before 1st January 1970. The negative long value represents the time before 1st January 1970




// Java program to demonstrate the
// use of getTime() 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());
  
        System.out.println("Time in milliseconds : "
                           + ts.getTime());
    }
}


Output:

Timestamp time : 1969-12-31 23:59:50.0
Time in milliseconds : -10000

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7103 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS