Calculating difference between two java.sql.Timestamp shows negative value

I am calculating the difference between two timestamps which is in the following format

2013-07-22 05:24:24.77

I am using the following method to calculate the difference between two timestamps

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date resDate = sdf.parse(responseData.getTimestamp().toString());
Date reqDate = sdf.parse(requestData.getTimestamp().toString());
System.out.println("response-->"+responseData.getTimestamp());
System.out.println("request-->"+requestData.getTimestamp());
System.out.println("diff-->"+(resDate.getTime()-reqDate.getTime()));

The difference between two timestamps is negative in the following cases. For example

Response date : 2013-07-22 05:24:24.77

Request date : 2013-07-22 05:24:24.663

Result is :

diff-->-586

it should subtract something like this "770-663" instead it subtracting the timestamp as "77-663".

Can anyone please suggest what changes I should make or is there any other way to do it??

Thanks in advance

Answers


SSS in SimpleDateFormat means the number of milliseconds, not fractional second, that is for SimpleDateFormat 663 > 77. But Timestamp.toString formats timestamp in yyyy-mm-dd hh:mm:ss.fffffffff format, where fffffffff is fractional second. Usejava.sql.Timestamp.valueOf(str) for parsing what Timestamp.toString produces


Need Your Help

Is Objective-C's NSMutableArray thread-safe?

objective-c ios multithreading nsmutablearray

I've been trying to fix this crash for almost a week. The application crashes without any exception or stack-trace. The application does not crash in any way while running through instruments in zo...

android 4.0.3 and jquery compatibility

android jquery

I'm currently developing an Android application who uses a WebView loading html/js pages.