Sritam Jagadev Sritam Jagadev - 8 months ago 29
Android Question

Convert given time in String format to seconds in Android

Suppose time is given in MM:SS(ex- 02:30) OR HH:MM:SS in String can we convert this time to second.


In your case, using your example you could use something like the following:

String time = "02:30"; //mm:ss
String[] units = time.split(":"); //will break the string up into an array
int minutes = Integer.parseInt(units[0]); //first element
int seconds = Integer.parseInt(units[1]); //second element
int duration = 60 * minutes + seconds; //add up our values

If you want to include hours just modify the code above and multiply hours by 3600 which is the number of seconds in an hour.