I have one String
String time = 1 hour 37 minutes How can I convert this time into 1:37:00 format in android?
I have one String
String time = 1 hour 37 minutes How can I convert this time into 1:37:00 format in android?
1)first step remove space from string (string = "1 hour 37 minutes)
text = string.replace(" ", "");
2) Second step replace hour to ":" and minutes by ":00"
text = text.replace("hour",":");
text = text.replace("minutes",":00");
I guess you must code a function that format your string into what you need. Your function should be like this:
public static String formatTime(String inputTime)
{
String result = inputTime.replace(" ","");
result = result.replace("hours",":").replace("hour",":").replace("minutes","").replace("minute","");
String[] resultAux = result.split(":"); //First position will be hours, second position will be minutes
for(int i=0; i<resultAux.length; i++)
{
if(resultAux[i].length() == 1)
{
resultAux[i] = "0" + resultAux[i]; //If we have 1 character in minutes we put the 0
}
}
return String.join(":",resultAux) + ":00"; //Due to in your example you don't mention that you can have miliseconds we just put the miliseconds at the end
}
Of course you will call your function like this:
System.out.println("result is: " + formatTime("2 hours 37 minutes"));