5 Replies Latest reply: Jan 18, 2013 8:16 AM by shreevat

# Calculation issues with date and time - Urgent

Hi,
I have to calculate the time difference between two dates with time stamps.

startTime = 17-01-2013 09:13:45 ;

endTime = 17-01-2013 09:13:46 ;

long finalTime = ( startTime - endTime) / 1000;

Above finalTime is not giving accurate time in seconds. it differs by 1 sec. For the above it should be 1 sec difference but giving valuse as zero. How to get the accurate time ? pl suggest.

I need finalTime in seconds.

Thanks,
• ###### 1. Re: Calculation issues with date and time - Urgent
Rbojja,

can you try the below code to get the mlilli seconds:
{
long millis = (long)((oracle.sql.DATE)systemJboDate).timestampValue().getTime();
}

Regards
Sridhar
• ###### 2. Re: Calculation issues with date and time - Urgent
Hi Sridhar,

Thanks for your code , issue is, in my code why there is a cal difference?
Can you pl suggest .
• ###### 3. Re: Calculation issues with date and time - Urgent
When you subtract date values directly, it gives you the difference in "days" provided date formatting is taken care. You will have to multiple the answer by 24*60*60 to convert it to seconds. See the below sql snippet which gives you a difference of 1 sec. You have use simpleDateFormat to convert the date to a acceptable format, subtract and multiple as needed. Hope this helps.

select (to_date('17-01-2013 09:13:46','DD-MM-RRRR HH24:MI:SS')- to_date('17-01-2013 09:13:45','DD-MM-RRRR HH24:MI:SS'))*(24*60*60) dt from dual

Thanks
Shree
• ###### 4. Re: Calculation issues with date and time - Urgent
Logic is working fine in sql, but in java its not the same.

Difference of time is coming as 1000 millseconds.

1000 * 24* 60 * 60 = 864000. which is not in seconds

Any others ways specific in java

Edited by: rbojja on Jan 18, 2013 9:32 AM
• ###### 5. Re: Calculation issues with date and time - Urgent
If you are getting the difference as 1000 milliseconds then the answer is correct. 1000 millisecond is 1 second.

Thanks
Shree