Question

I have bunch of dates in a format like Jan. 14,2014,Apr. 20,2014,Aug. 14,2014 etc,. which are extracted from a set of PDF documents.

My Problem
I added the above dates to a mysql column with Column Datatype as Date using java (by PreparedStatement).

....
st.SetDate(3,"Jan. 14,2014");
....

I added the datas using ExecuteQuery.

But when program executes an error message returned by MySql stating that the date formats are incompatible with MySql column type) Date..

My Question
How to convert this above mentioned date formats into mysql compatible Date formats ?

Was it helpful?

Solution

By your current posted code:

st.SetDate(3,"Jan. 14,2014");

This does not even compile. You could try getting a new Date object from a String (since this is what you're trying to accomplish), so use a SimpleDateFormat for this:

SimpleDateFormat sdf = new SimpleDateFormat("MMM. dd,yyyy");
Date date = sdf.parse("Jan. 14,2014");
st.setDate(3, new java.sql.Date(date.getTime()));
//rest of your code...

Similar to this, you can parse time or date and time into a java.util.Date using SimpleDateFormat and then convert it to the respective class java.sql.Time and java.sql.Timestamp using date.getTime().

Also note that you can p̶a̶s̶s̶ retrieve a java.util.Date object reference to PreparedStatement#getDate (and getTime and getTimestamp) since java.sql.Date, java.sql.Time and java.sql.Timestamp extend from java.util.Date. For more info, please refer here: Date vs TimeStamp vs calendar?

OTHER TIPS

Assuming the column type supports a Date value, you could use a SimpleDateFormat to parse the String values to a java.util.Date and create a java.sql.Date which can be applied to the setDate method...

SimpleDateFormat sdf = new SimpleDateFormat("MMM. dd,yyyy");
Date date = sdf.parse("Jan. 14,2014");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());

Check it SimpleDateFormat for more details

One possible solution is to use the String datatype instead of date in your table.

Use SimpleDateFormat to get the string representation of the date to a Date Object. This date object can then be used to feed the set date method of the prepared statement.

SimpleDateFormat sdf = new SimpleDateFormat(....)
java.util.Date date = sdf.parse(....);

preparedStmt.setDate(...., date);

first convert the java.util.Date to java.sql.Date then try to set the Java.sql.Date

you can use this logic to convert

If your date is String then first convert it into Java.util.Date type either by using the SimpleDateFormat or DateFormat

If u want to use a DateFormat you can use it also: But this changes the expected date format depending on the locale settings of the machine it's running on.

If you have a specific date format, you can use SimpleDateFormat:

 Date d = new SimpleDateFormat("MMM. dd,yyyy").parse("Jan. 14,2014");

     java.sql.Date sqlDate = new java.sql.Date(d.getTime());

I don't really think this all java stuff is necessary. You can simply use this very easy sql process to insert date: NOW() in mysql query like INSERT INTO table_name(c1,c2,datecolumn) VALUES('value1','value2',NOW()); It is much simplier I think :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top