How to convert a timestamp object in to Date in JDBC program?

JDBCJava 8Object Oriented ProgrammingProgramming

The getTime() method of the Timestamp class retrieves and returns the time from the current timestamp in milliseconds (long) from epoch time 1, 1970 00:00:00.000 GMT.

Timestamp timestamp = rs.getTimestamp("DispatTimestamp");
long time = timestamp.getTime();

The constructor of the java.sql.Date class accepts a long variable representing the time in milliseconds from the epoch time and constructs the date object.

//Printing the date of dispatch
System.out.println("Date of dispatch: "+new Date(time));

Using these, you can convert a TimeStamp object to Date object in JDBC.

Assume we have established connection with MySQL database and created a table named dispatch_data using statement object as:

//Creating a Statement object
Statement stmt = con.createStatement();

//Query to create a table
String create_query = "Create table disptach_data ("
   + "Product_Name VARCHAR(255), "
   + "Name_Of_Customer VARCHAR(255) , "
   + "Dispatch_Timestamp timestamp, "
   + "Location VARCHAR(255) )";

We have populated the table using PreparedStatement as:

String query = "INSERT INTO dispatch_data VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);

pstmt.setString(1, "KeyBoard");
pstmt.setString(2, "Amith");
pstmt.setTimestamp(3, new Timestamp(1567296000000L));
pstmt.setString(4, "Hyderabad");

pstmt.setString(1, "Earphones");
pstmt.setString(2, "Sumith");
pstmt.setTimestamp(3, new Timestamp(1556668800000L));
pstmt.setString(4, "Vishakhapatnam");

pstmt.setString(1, "Mouse");
pstmt.setString(2, "Sudha");
pstmt.setTimestamp(3, new Timestamp(1551398399000L));
pstmt.setString(4, "Vijayawada");

System.out.println("Records inserted......");

Following JDBC program retrieves the timestamp values from the ResultSet converts into Date and Time objects and prints the details.

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
public class TimeStampToDate {
   public static void main(String args[])throws Exception {
      //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();
      //Creating Statement object
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatch_data");
      //Retrieving values
      while( {
         System.out.println("Product Name: "+rs.getString("Product_Name"));
         System.out.println("Name Of The Customer: "+rs.getString("Name_Of_Customer"));
         //Retrieving the timestamp
         Timestamp timestamp = rs.getTimestamp("Dispatch_Timestamp");
         //Printing the date of dispatch
         System.out.println("Date of dispatch: "+new Date(timestamp.getTime()));
         //Printing the time of dispatch
         System.out.println("Time Of Dispatch: "+new Time(timestamp.getTime()));


Connection established......
Product Name: KeyBoard
Name Of The Customer: Amith
Date of dispatch: 2019-09-01
Time Of Dispatch: 05:30:00

Product Name: Ear phones
Name Of The Customer: Sumith
Date of dispatch: 2019-05-01
Time Of Dispatch: 05:30:00

Product Name: Mouse
Name Of The Customer: Sudha
Date of dispatch: 2019-03-01
Time Of Dispatch: 05:29:59
Published on 02-Apr-2019 08:39:20