-2

like i have 5 Times

1. 05:12:02
2. 19:12:52
3. 40:12:14
4. 56:54:10
5. 41:12:12
-----------
Total Seconds : 0#####..`
-----------

i want like this, how can i , please help me .

can I use this? :

 public String addTime(int hour, int minute, int minutesToAdd) {
    Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
    calendar.add(Calendar.MINUTE, minutesToAdd);
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    String date = sdf.format(calendar.getTime());
    return date;
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Welcome to SO. Please provide a Minimal, Complete, and Verifiable example. **Show us the code for your latest attempt** and where you got stuck. and explain why the result is not what you expected. Edit your question to include the code, please don't add it in a comment, as it will probably be unreadable. https://stackoverflow.com/help/mcve – Dragonthoughts Sep 13 '18 at 10:01
  • whats the problem then? convert those 5 times to seconds and add them up – pskink Sep 13 '18 at 10:06
  • @pskink how ? have code ? –  Sep 13 '18 at 10:09
  • either by using `SimpleDateFormat` or by: seconds = hrs * 3600 + min * 60 + sec – pskink Sep 13 '18 at 10:11

2 Answers2

2

Use the Duration class from java.time (the modern Java date and time API):

    String[] times = {
            "05:12:02",
            "19:12:52",
            "40:12:14",
            "56:54:10",
            "41:12:12"
    };
    Duration timeSum = Duration.ZERO;
    for (String time : times) {
        // reformat to ISO 8601
        time = time.replaceFirst("(\\d{2}):(\\d{2}):(\\d{2})", "PT$1H$2M$3S");
        // add
        timeSum = timeSum.plus(Duration.parse(time));
    }
    System.out.println("Total seconds: " + timeSum.getSeconds());

Output:

Total seconds: 585810

The Duration class cannot directly parse your time strings. It parses ISO 8601 standard format, so I use a regular expression for converting 05:12:02 to PT05H12M02S. Then I feed this into Duration.parse. You may read the ISO 8601 string as “a period of time of 05 hours 12 minutes 02 seconds”.

Classes meant for dates and times — Date, Calendar, LocalTime, etc. — are ill suited for amounts of time. Date and Calendar are furthermore long outdated and poorly designed, so don’t try those. While it wouldn’t be impossible to get through, there are some pitfalls, and even if you succeed, it will be hard to read the code and convince oneself that it is correct.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom). The code above was developed and run with org.threeten.bp.Duration from the backport.
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Could it parse a time with milliseconds included like: `String[] times = {"05:12:02.002","19:12:52.947"}`? I tried with `"PT$1H$2M$3S$4Z"` but I get java.time.format.DateTimeParseException: Text cannot be parsed to a Duration. Same for `PT$1H$2M$3S$` – dazito Sep 25 '18 at 10:59
  • 1
    @dazito If you change the line with the regular expression to `time = time.replaceFirst("(\\d{2}):(\\d{2}):(\\d{2}(?:\\.\\d+)?)", "PT$1H$2M$3S");` it will also accept times with and without decimals on the seconds. `(?:…)` is a non-capturing group, and the question mark after it says that it can be there 0 or 1 time. – Ole V.V. Sep 25 '18 at 11:01
0

A easy way to do it, if the format is the one you have pointed, without have to worry in convert the Date is the following:

int totalSum = getSecondsFromDate("05:12:02") + getSecondsFromDate("19:12:52") + getSecondsFromDate("40:12:14") + getSecondsFromDate("56:54:10") + getSecondsFromDate("41:12:12");

private int getSecondsFromDate(String date){
  //We split the date to have hours, minutes and seconds
  String splitDate = date.split(":");
  int hours = Integer.parseInt(splitDate[0]);
  int minutes = Integer.parseInt(splitDate[1]);
  int seconds = Integer.parseInt(splitDate[2]);

  int totalSeconds = (hours*60*60) + (minutes*60) + seconds;
  return totalSeconds;
}
Alex Bean
  • 493
  • 7
  • 19
  • 1
    I'd suggest using `LocalTime` class and `Duration` class rather than roll-your-own. – Basil Bourque Sep 13 '18 at 20:23
  • Yes, that's definitely what I would do if I am expecting different kind of fomats in the dates. However in this case, if is always the same is fine. – Alex Bean Sep 14 '18 at 11:53