I am trying to calculate the sum of a list of time duration for an attendance app. I was able to calculate the difference of 2 times but not sure how to traverse through the list to add the dates together.
The below code is what I have attempted so far. There are 2 time durations in the list and they are added to the list from Firebase using an object class.
Duration 1 = 0:5:42
Duration 2 = 0:0:09
Expected Total = 0:5:51
//initialized
long sum;
//current method attempted
public void grandTotal() throws ParseException {
        java.util.Date date1;
        java.util.Date date2;
        DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
        for(int i = 0 ; i < newList.size(); i++) {
            String str_time1 = newList.get(i).getDuration();
            date1 = formatter.parse(str_time1);
            for (int j = 1; j < newList.size(); j++) {
                String str_time2 = newList.get(j).getDuration();
                date2 = formatter.parse(str_time2);
                sum = date1.getTime() + date2.getTime();
            }
        }
        secs = sum/1000;
        mins = secs/60;
        secs%=60;
        hours = mins/60;
        mins%=60;
        txtDailyBalance.setText(hours+":"+mins+":"+String.format("%02d",secs));
    }
Result output I get is = -1:59:42
I got the solution using this method but I don't think this is good practice so I have been looking into learning more about the different methods in this post so thank you.
//Sum of time durations for that day
public void grandTotal() throws ParseException {
    java.util.Date date1;
    java.util.Date date2;
    DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
    for(int i = 0 ; i < newList.size(); i++) {
        timeDuration.add(newList.get(i).getDuration());
    }
    long tDur = 0;
    for (String tmp : timeDuration){
        String[] arr = tmp.split(":");
        tDur += Integer.parseInt(arr[2]);
        tDur += 60 * Integer.parseInt(arr[1]);
        tDur += 3600 * Integer.parseInt(arr[0]);
    }
    long hours = tDur / 3600;
    tDur %= 3600;
    long mins = tDur / 60;
    tDur %= 60;
    long secs = tDur;
    txtDailyBalance.setText(hours+":"+mins+":"+String.format("%02d",secs));
}
 
     
     
    