Java sql.Timestamp toString() method with example

JDBCJava 8Object Oriented ProgrammingProgramming

The toString() method of the java.sql.Timestamp class returns the JDBC escape format of the time stamp of the current Timestamp object as String variable.

i.e. using this method you can convert a Timestamp object to a String.

//Retrieving the Time object
Timestamp timestampObj = rs.getTimestamp("DispatchTimeStamp");
//Converting the Time object to String format
String time_stamp = timestampObj.toString();


Let us create a table with the name dispatches_data in MySQL database using CREATE statement as shown below:

CREATE TABLE dispatches_data(
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchTimeStamp timestamp,
   Price INT,
   Location VARCHAR(255));

Now, we will insert 5 records in dispatches_data table using INSERT statements:

insert into dispatches_data values('Key-Board', 'Raja', TIMESTAMP('2019-05-04', '15:02:45'), 7000, 'Hyderabad');
insert into dispatches_data values('Earphones', 'Roja', TIMESTAMP('2019-06-26', '14:13:12'), 2000, 'Vishakhapatnam');
insert into dispatches_data values('Mouse', 'Puja', TIMESTAMP('2019-12-07', '07:50:37'), 3000, 'Vijayawada');
insert into dispatches_data values('Mobile', 'Vanaja' , TIMESTAMP ('2018-03-21', '16:00:45'), 9000, 'Chennai');
insert into dispatches_data values('Headset', 'Jalaja' , TIMESTAMP('2018-12-30', '10:49:27'), 6000, 'Goa');

Following JDBC program establishes a connection with the database and retrieves the contents of the dispatches_data table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

public class Timestamp_toString {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Query to retrieve the contents of the dispatches_data table
      String query = "select * from dispatches_data";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      while ( {
         System.out.println("Product Name: " + rs.getString("ProductName"));
         System.out.println("Customer Name: " + rs.getString("CustomerName"));
         Timestamp timeStampObj = rs.getTimestamp("DispatchTimeStamp");
         //Converting the Time object to String format
         String timeStamp = timeStampObj.toString();
         System.out.println("Dispatch time stamp in String format: " + timeStamp);
         System.out.println("Location: " + rs.getString("Location"));

Here, in this program while retrieving the column values, we have converted the DeliveryTime value from the Timestamp object to string format using the toString() method of the Timestamp class and trying to display it.


Connection established......
Product Name: Key-Board
Customer Name: Raja
Dispatch time stamp in String format: 2019-05-04 15:02:45.0
Location: Hyderabad
Product Name: Earphones
Customer Name: Roja
Dispatch time stamp in String format: 2019-06-26 14:13:12.0
Location: Vishakhapatnam
Product Name: Mouse
Customer Name: Puja
Dispatch time stamp in String format: 2019-12-07 07:50:37.0
Location: Vijayawada
Product Name: Mobile
Customer Name: Vanaja
Dispatch time stamp in String format: 2018-03-21 16:00:45.0
Location: Chennai
Product Name: Headset
Customer Name: Jalaja
Dispatch time stamp in String format: 2018-12-30 10:49:27.0
Location: Goa
Updated on 30-Jul-2019 22:30:26