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

     

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 {
Class.forName("com.mysql.jdbc.Driver");
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 (rs.next()) {
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");
System.out.println(timeConverter);
System.out.println(dateConverter);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Output :

mysqlTime=12:20:10
mysqlDate=2009-03-01
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