[Solved] How to sum times in java [closed]


Creating DateTime variables and summing them is an approach, but I assume you’re new to Java and give you basic answer to warn you up for some coding and not to go the easy way:

String[] timesSplit = times.split(" | ");

int hour = 0;
int minute = 0;

for(int i = 0; i < timesSplit.length; i += 2) {
    String[] time = timesSplit[i].split(":");
    hour += Integer.parseInt(time[0]);
    minute += Integer.parseInt(time[1]);
}

hour += minute / 60;
minute %= 60;

String result = hour + ":" + minute;

EDIT: Strange behaviour, splitting for ” | ” results String array like:

5:33, |, 1:32, ..

So edited the for increment statement accordingly. You can check the running code here.

solved How to sum times in java [closed]