Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

Android : how can I add 5 Times in a variable and get total seconds of them

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...