John Laza
John Laza

Reputation: 1

date type in java to fill a MySQL database

i have a field in my database (MySQL) with date type (with format yyyy-mm-dd) .I dont what data type to use to assing the date type of MySql to Java . Also i have to convert a string to this type .

Upvotes: 0

Views: 552

Answers (1)

JB Nizet
JB Nizet

Reputation: 692121

Use a java.sql.Date to get the value of a date column:

java.sql.Date date = resultSet.getDate("theDateColumn");

To convert a string to a java.sql.Date, use a DateFormat to parse the string into a java.util.Date, and then transform the java.util.Date into a java.sql.Date:

String s = "01/28/2013";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date d = df.parse(s);
java.sql.Date date = new java.sql.Date(d.getTime());

Upvotes: 1

Related Questions