Mustafa Güven
Mustafa Güven

Reputation: 15744

How to sum minutes in array if their format is string?

I have an string array that includes some minutes like "00:05", "00:30", "00:25" etc. I want to sum the values as time format? Can anyone help me how do I do this?

Upvotes: 1

Views: 800

Answers (6)

Alejandro Diaz
Alejandro Diaz

Reputation: 431

An object oriented approach:

public static TimeAcumm sum(final String[] times) {
    final TimeAcumm c = new TimeAcumm();
    for (final String time : times) {
        c.incrementFromFormattedString(time);
    }

    return c;
}

public class TimeAcumm {
    private int hours = 0;
    private int minutes = 0;
    private int seconds = 0;

    public int getHours() {
        return hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getSeconds() {
        return seconds;
    }

    public void incrementFromFormattedString(final String time) {
        final String[] parts = time.split(":");
        this.minutes += Integer.parseInt(parts[0]);
        this.seconds += Integer.parseInt(parts[1]);
        validate();
    }

    private void validate() {
        if (this.minutes > 59) {
            this.hours++;
            this.minutes -= 60;
        }

        if (this.seconds > 59) {
            this.minutes++;
            this.seconds -= 60;
        }
    }

    @Override
    public String toString() {
        final String s = hours + "H:" + minutes + "M:" + seconds + "S";
        return s;
    }
}

Upvotes: 0

emory
emory

Reputation: 10891

this is my suggestion. Neither compiled, ran, tested, nor guaranteed.

long seconds =  0; 
for ( String min : minutes )
{
    seconds += Integer.parseInt(min.substring(0,1))*60 + Integer.parseInt(min.substring(3,4));
}
return new Date ( seconds / 1000 ) ;

Upvotes: 0

Wayne
Wayne

Reputation: 60414

Total time in minutes:

int sum = 0;
final String[] mins = new String[] { "00:05", "00:30", "00:25" };
for (String str : mins) {
    String[] parts = str.split(":");
    sum += Integer.parseInt(parts[1]);
}
System.out.println(sum);

You don't specify exactly how you want this output formatted.

If there may be hour elements as well, then replace the second line of the loop with this:

sum += (Integer.parseInt(parts[0]) * 60) + Integer.parseInt(parts[1]);

Upvotes: 1

Evan Davis
Evan Davis

Reputation: 36592

Split the strings on ':', pars the values as ints and add 'em up.

Upvotes: 0

Brigand
Brigand

Reputation: 86250

You could substring it, and then call Integer.parseInt on the result. For the hours part, do the same and multiply it by 60.

Upvotes: 0

Robin
Robin

Reputation: 36611

I'll go for quick and dirty

  1. Split each String on the ":"
  2. Convert both parts to integer
  3. Multiply the first time by 60 to convert hours to minutes, and add the second part
  4. Do this for each value in your array, and count them together
  5. This results in the total time in minutes, which you can convert to whatever format you like

Upvotes: 1

Related Questions