Error Timestamp Format Must Be Yyyy-Mm-Dd Hh: Mm: Ss[. Fffffffff] While Saving Timestamp into Databse
Simpledateformat Sdf = New Simpledateformat("Yyyy-Mm-Dd Hh: Mm: Ss a Z"); Sdf. setTimeZone(TimeZone. getTimeZone("US/Eastern")); String Datetostoreindatabase=...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss a z");
sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
String DateToStoreInDataBase= sdf.format(obj1.getSomeDate().toGregorianCalendar().getTime());
System.out.println(emprSubDte);
Timestamp ts = Timestamp.valueOf(emprSubDte);
preparedStatement.setTimestamp(72,ts);
sysout of DateToStoreInDataBase is = " 2014-19-13 12:19:59 PM EDT" when i am trying to save this result into database in am getting error Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff].
I have the same format but still i am reciving the error.
4 Answers
The problem is in your SimpleDateFormat instantiation. You're using this pattern: "yyyy-mm-dd hh:mm:ss a z"
The issue is in the month. It should be "MM" instead of "mm". "mm" is for "minute in the hour".
Reference:
Must Read
What went wrong with your code
Timestamp#valueOf accepts the timestamp string in the format yyyy-[m]m-[d]d hh:mm:ss[.f...] while your timestamp string, DateToStoreInDataBase has the value 2014-19-13 12:19:59 PM EDT i.e. it has two other things PM (AM/PM marker) and EDT (time zone) - none of which are supported by this parameter.