Sritam Jagadev Sritam Jagadev - 2 years ago 105
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 format.how can we convert this time to second.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download