# Thread: How to calculate the sum of time

1. Member
Join Date
Jul 2010
Posts
37
Rep Power
0

## How to calculate the sum of time

Hello All,

I am having a jsp which is reading the total hours for each employee. However, I am reading their total hours from the DB and would like to sum the time at the grand total.

I tried to use:
Java Code:

Time total=rs.getTime("totalhours");
Time grandtotaltime =(00:00:00);
grandtotaltime = grandtotaltime + total;
But, an error is occurred that can not sum the time variable. So, how can I do that?

My code in the jsp is:

Java Code:
while (rs.next())

{
Time total=rs.getTime(totalhours");
%>
<tr>
<td>
<%=total%>
</td>
</tr>
%>
}
<tr>
<td>
Grand Total =
</td>
</tr>
An example of the output:

7:15:00
0015:00

What I would like is
Grand Total = 7:30:00

Thanks for the help
Last edited by maas; 08-04-2011 at 11:49 PM.

2. The Calendar class has an add method. You could try converting your time objects and using Calendar instead.

Or you will just have to roll your own.

3. Member
Join Date
Jul 2010
Posts
37
Rep Power
0
Can you please show me in an example?

4. No, don't use Calendar. Calendar, util.Date, sql.Date and Time are all representations of an instant in time, not an interval.

You might want to take a look at Joda-Time which has a class Interval which
represents a period of time between two instants.
db

edit The still doesn't solve the issue of adding time intervals, sorry. It's probably better you roll your own -- add the times' millisecond value as longs.

5. Member
Join Date
Jul 2010
Posts
37
Rep Power
0
Thanks to your help.

But, can you please explain more since I am a beginner in Java

6. My bad. Misunderstood. Thought OP was trying to advance a certain period of time.

Maas what Daryl meant is that you should convert your time into seconds or milliseconds. So 1 hour = 3600 seconds. If you do that to all your times you can then add them up and then convert the result back to hours, minutes and seconds.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•