Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert given time in String format to seconds in Android

Tags:

java

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.

like image 665
Sritam Jagadev Avatar asked Mar 26 '15 07:03

Sritam Jagadev


People also ask

How do you convert HH MM SS to seconds in Java?

const d = '02:04:33'; // your input string const a = hms. split(':'); // split it at the colons const result = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); console. log(result); Try Lightrun to collect production stack traces without stopping your Java applications!

How do I convert string to minutes?

Solution 1. Split the string into its component parts. Get the number of minutes from the conversion table. Multiply that by the number and that is the number of minutes.

How do you convert kotlin to milliseconds to seconds?

Example 2: Convert Milliseconds to Minutes and Seconds fun main(args: Array<String>) { val milliseconds: Long = 1000000 val minutes = milliseconds / 1000 / 60 val seconds = milliseconds / 1000 % 60 println("$milliseconds Milliseconds = $minutes minutes and $seconds seconds.") }


1 Answers

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.

like image 69
CodeCamper Avatar answered Nov 15 '22 16:11

CodeCamper