Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Sqldate Mysql Date To Java Date

Related Tutorials


 
 

Share on Google+Share on Google+

Mysql Date To Java Date

Advertisement
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 {
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
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: January 21, 2009

Related Tutorials

Discuss: Mysql Date To Java Date   View All Comments

Post your Comment


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

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!
DMCA.com