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= 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.

0

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:

1

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.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.