Share on Google+Share on Google+

Mysql Date To Java Date

Mysql Date To Java Date retrieve the records from Mysql and display the date in java.

Mysql Date To Java Date


Mysql Date To Java Date retrieve the records from Mysql and display the date in java.

Understand with Example

The Tutorial illustrate an example from 'Mysql Date To Java Date'. To understand and grasp the example we create a table 'mysql datetable', insert the records and display the records  from 'mysqldatetable' in java.

This Example display date from mysql in java:

select * from mysqldatetable:




| time_field | date_field | date_time_field |
| 12:20:10 | 2009-03-01 | 2009-01-03 12:20:10 |

 Source Code :

import java.sql.*;
public class DateToJava {
public static void main(String args[]) {
ResultSet rs = null;
Connection con = null;
Statement stmt = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_register","root","root");
stmt = con.createStatement();
rs = stmt.executeQuery("select time_field, date_field, date_time_field from mysqldatetable");
while ( {
java.sql.Time mysqlTime = rs.getTime(1);
java.sql.Date mysqlDate = rs.getDate(2);
java.sql.Timestamp mysqlTimestamp = rs.getTimestamp(3);
System.out.println("mysqlTime=" + mysqlTime);
System.out.println("mysqlDate=" + mysqlDate);
System.out.println("mysqlTimestamp=" + mysqlTimestamp);

java.util.Date timeConverter = new java.util.Date(mysqlTime.getTime());
java.util.Date dateConverter = new java.util.Date(mysqlDate.getTime());
System.out.println("Standard Date");
} catch (Exception e) {
} finally {
try {
} catch (SQLException e) {

Output :

mysqlTimestamp=2009-01-03 12:20:10.0
Standard Date
Thu Jan 01 12:20:10 GMT+05:30 1970
Sun Mar 01 00:00:00 GMT+05:30 2009


Posted on: January 21, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Mysql Date To Java Date   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Alonso Hurter
February 7, 2013

in the example i want to format mysqlTimestamp, you did with Time and Date, but what about Timestamp? dont want "2009-01-03 12:20:10.0" string... thanks!