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;
}
Use the
Duration
class from java.time (the modern Java date and time API):Output:
The
Duration
class cannot directly parse your time strings. It parses ISO 8601 standard format, so I use a regular expression for converting05:12:02
toPT05H12M02S
. Then I feed this intoDuration.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
andCalendar
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.org.threeten.bp.Duration
from the backport.org.threeten.bp
with subpackages.Links
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).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: