I have a date/time value in String form like "11/28/2017 4:00:49 PM" in my java code. Now how can I take this same value to MySQL column of type datetime?
Please note, I'm using PreparedStatement to insert the record in MySQL DB.
Here are the few ways I tried...
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("MM-dd-yyyy KK:mm:ss a");
ps.setString(outputFormat.format(inputFormat.parse(rs.getString(1))));
I used the above code and I'm getting the below exception...
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '08-09-0017 02:46:57 AM' for column 'column_name' at row 1
If I use the below code, I'm getting only Date but time with AM/PM is lost
ps.setDate(inputFormat.parse(rs.getString(1)));
If I use the below code, I'm getting only Time but without AM/PM and Date
ps.setTime(inputFormat.parse(rs.getString(1).getTime()));
If I use the below code, I get error
ps.setDate(1, (java.sql.Date) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("11/28/2017 4:00:49 PM"));
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date