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

tom
  • 5,114
  • 6
  • 24
  • 36

4 Answers4

3

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: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

June
  • 592
  • 8
  • 18
  • Thank you for pointing out, After changing i am getting number format exception now – tom Oct 13 '14 at 17:07
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.

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

here is my working successful code :

com.google.firebase.Timestamp currentTimeStamp = com.google.firebase.Timestamp.now();
Date ServDate = currentTimeStamp.toDate();
String currentDateStringFormated = new SimpleDateFormat("dd/MM/yyyy").format(ServDate);
Ayman.dev
  • 51
  • 12
-1

check if there is incompatible setter or getter , either form your database model side , view pages , beans to xhtml mapping. I found date value for float and fixed it.

Develop4Life
  • 7,581
  • 8
  • 58
  • 76