- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java sql.Timestamp toString() method with example
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();
Example
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 (rs.next()) { 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")); System.out.println(); } } }
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.
Output
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
- Related Articles
- Java sql.Timestamp valueOf() method with example
- Java sql.Date toString() method with example?
- Java sql.Time toString() method with example
- Java Signature toString() method with Examples
- Java toString() method.
- Pattern toString() method in Java with examples
- Matcher toString() method in Java with Examples
- Java Signature toString() method
- ShortBuffer toString() method in Java
- Provider toString() method in Java
- Duration toString() method in Java
- MonthDay toString() Method in Java
- Instant toString() method in Java
- LocalDate toString() method in Java
- LocalDateTime toString() method in Java
