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.
java.time
The java.time
API, released with Java-8 in March 2014, supplanted the error-prone legacy date-time API. Since then, using this modern date-time API has been strongly recommended. Note that the class, java.sql.Timestamp
, not only inherited those problems from its parent, java.util.Date
but also introduced many awful hacks.
Solution using java.time API
If you scroll down to the bottom of this page (the same link shared above), you will find the support for java.time
API in JDBC.
Assuming your database column is of type, TIMESTAMP WITH TIMEZONE
, the solution would be
OffsetDateTime odt = ZonedDateTime.now(ZoneId.of("America/New_York"))
.toOffsetDateTime();
preparedStatement.setObject(72, odt);
If the column is of type, TIMESTAMP
, the solution would be
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("America/New_York"));
preparedStatement.setObject(72, ldt);
Learn more about the modern Date-Time API from Trail: Date Time.