Q:

retaining time stamp hour value while conversion from String to Timestamp

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?

A:

@Nikunj this new java.text.SimpleDateFormat("yyyy-MM-dd"); is literally losing your timestamp...so it sets the time into 12AM.... you need to get the precise format.
Can you please try this, Assuming your string contains the correct date from source database,
String sourceDate = "8/21/2012 2:09";
Timestamp sourceTS = Timestamp.valueOf(sourceDate);
System.out.println(sourceTS.getNanos());

Here is the post: Convert Java String to sql.Timestamp.

