I have with me an epoch time, which i would like to convert to an sql timestamp. I can extract the actual time from the epoch using this code :
String time = "1351504294";
long t = Long.parseLong(time);
Timestamp ts = new Timestamp(t*1000);
The output I’m getting is : 2012-10-29 09:58:50.0.
But when i try to insert this into a table, it shows error because of the millisecond part, ’09:58:50.0′. How can I remove the millisecond part from the timestamp?
If you are adding the
Timestampdirectly to the SQL statement then Java is calling thetoString()function wich always outputs the formatyyyy-mm-dd hh:mm:ss.fffffffff. There is nothing that you could do to theTimestampobject that would eliminate the nanoseconds part.If you want just the
yyyy-MM-dd hh:mm:ssportion you could either do:Or you could use
SimpleDateFormat: