I have timestamp read from database into a string as below:
Source database table timestamp field value is as below:
8/21/2012 2:09 AM
I am reading above timestamp into a String field in a java program.
However, while writing it into destination database table, I am reconverting the String back into time stamp using below code:
java.text.DateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date newDate = format.parse(item.getMyTimeStamp());
ps.setTimestamp(1, new java.sql.Timestamp(newDate.getTime()));
However, the timestamp field value in the destination table is coming as below:
8/21/2012 12:00 AM
As clear the hour field is getting reset to 12 AM.
How can I retain the original timestamp value?
@Nikunj this
new java.text.SimpleDateFormat("yyyy-MM-dd");is literally losing yourtimestamp…so it sets the time into12AM…. you need to get the precise format.Can you please try this, Assuming your string contains the correct date from source database,
Here is the post: Convert Java String to sql.Timestamp.