tags:

views:

187

answers:

2

Currently I have a function which can take the start time and end time of one day, and calculate the difference between the two, giving me the hours worked in a day. What I would like to do is be able to get the hours worked for 7 days, and return a grand total, while remaining with the display format (HH:mm).

My function for a single day's total:

Period p = new Period(this.startTime[dayIndex], this.endTime[dayIndex]);
long hours = p.getHours();
long minutes = p.getMinutes();

String format = String.format("%%0%dd", 2);//Ensures that the minutes will always display as two digits.

return Long.toString(hours)+":"+String.format(format, minutes);

this.startTime[] & this.endTime[] are both arrays of DateTime objects.

Any suggestions?

+1  A: 

You'll need something to hold a week's worth of days, and call your function once for each day.

But that means you'll want to refactor so that your calculator method doesn't format as a string, but instead returns a numeric value, so you can easily add them together.

CPerkins
A: 

Solution I ended with for those interested

    Period[] p=new Period[7];
    long hours = 0;
    long minutes =0;
    for(int x=0; x<=this.daysEntered;x++)
    {
        p[x] = new Period(this.startTime[x], this.endTime[x]);
        hours += p[x].getHours();
        minutes += p[x].getMinutes();
    }

    hours += minutes/60;
    minutes=minutes%60;

    String format = String.format("%%0%dd", 2);

    return Long.toString(hours)+":"+String.format(format, minutes);
clang1234
`format` can be replaced by `"%02d"`.
BalusC