Results 1 to 6 of 6
  1. #1
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    29
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    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. #3
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    29
    Rep Power
    0

    Default

    Can you please show me in an example?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    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. #5
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks to your help.

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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    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

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Need help to calculate avg
    By vavavoom1 in forum New To Java
    Replies: 2
    Last Post: 04-02-2011, 07:42 PM
  3. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  4. calculate fft
    By ram.west in forum Advanced Java
    Replies: 2
    Last Post: 08-27-2008, 03:05 AM
  5. Calculate what e1 and e2 should be
    By Legoland in forum New To Java
    Replies: 11
    Last Post: 07-02-2007, 06:01 PM

Posting Permissions

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