
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Timestamp Object to Date in JDBC Program
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 dis System.out.println("Date of dis: "+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 dis_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) , " + "Dis_Timestamp timestamp, " + "Location VARCHAR(255) )"; stmt.execute(create_query);
We have populated the table using PreparedStatement as:
String query = "INSERT INTO dis_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.execute(); pstmt.setString(1, "Earphones"); pstmt.setString(2, "Sumith"); pstmt.setTimestamp(3, new Timestamp(1556668800000L)); pstmt.setString(4, "Vishakhapatnam"); pstmt.execute(); pstmt.setString(1, "Mouse"); pstmt.setString(2, "Sudha"); pstmt.setTimestamp(3, new Timestamp(1551398399000L)); pstmt.setString(4, "Vijayawada"); pstmt.execute(); 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 dis_data"); //Retrieving values while(rs.next()) { 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("Dis_Timestamp"); //Printing the date of dis System.out.println("Date of dis: "+new Date(timestamp.getTime())); //Printing the time of dis System.out.println("Time Of Dis: "+new Time(timestamp.getTime())); System.out.println(); } } }
Output
Connection established...... Product Name: KeyBoard Name Of The Customer: Amith Date of dis: 2019-09-01 Time Of Dis: 05:30:00 Product Name: Ear phones Name Of The Customer: Sumith Date of dis: 2019-05-01 Time Of Dis: 05:30:00 Product Name: Mouse Name Of The Customer: Sudha Date of dis: 2019-03-01 Time Of Dis: 05:29:59