Results 1 to 6 of 6
Thread: How to calculate the sum of time
- 08-05-2011, 12:47 AM #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;
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>
7:15:00
0015:00
What I would like is
Grand Total = 7:30:00
Thanks for the helpLast edited by maas; 08-05-2011 at 12:49 AM.
- 08-05-2011, 01:25 AM #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.
- 08-05-2011, 11:31 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 37
- Rep Power
- 0
Can you please show me in an example?
- 08-05-2011, 12:58 PM #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.
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.
- 08-05-2011, 02:00 PM #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
- 08-08-2011, 03:23 AM #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.
Similar Threads
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 01:46 PM -
Need help to calculate avg
By vavavoom1 in forum New To JavaReplies: 2Last Post: 04-02-2011, 08:42 PM -
calculate time diff for particular time period
By baktha.thalapathy in forum New To JavaReplies: 2Last Post: 05-24-2010, 05:10 PM -
calculate fft
By ram.west in forum Advanced JavaReplies: 2Last Post: 08-27-2008, 04:05 AM -
Calculate what e1 and e2 should be
By Legoland in forum New To JavaReplies: 11Last Post: 07-02-2007, 07:01 PM
Bookmarks