Date related problem

I've Fromdate and Todate stored in SQL 2000 server, wherein both are of DateTime field.

Through my application written in jsp, I insert both date and time in each of these fields

I want the difference of these two dates in the form of Hours. I tried DateDiff function of SQL Server, but it gives 0, if both FromDate and ToDate are having the same day.

i.e if FromDate is '23-05-2007 11:30::00'and

ToDate is '23-05-2007 12:30:00', I need to get the difference as 1 hour, but it is returning 0.

Is there any other function in SQL, which I can use in query OR is there any other method in jsp to achieve this task, after fetching the results from database?

[697 byte] By [harinibiligiria] at [2007-11-27 5:14:13]
# 1

How are you invoking the datediff? You need to tell it that it have to calculate the difference in hours (or minutes) and not in days for example.

Doing it in Java is straightforward. Retrieve the difference between the two Date (or Calendar) objects in milliseconds and recalculate it to hours.

BalusCa at 2007-7-12 10:36:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

You can get the difference between the two dates in milliseconds, that can be converted to hours.

Use Date.getTime()

It returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

long timeDiff = Date1.getTime() - Date2.getTime();

//Then convert the same from milliseconds to hours

Float timeInHours = new Float(timeDiff/1000*3600);

SirG

SirGenerala at 2007-7-12 10:36:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...