I want to create a library because I couldn't find one to convert seconds or milliseconds into time. By time I mean:
1) If I have 61 seconds the time format would be: 1:01 (not 1:1)
2) If I have equivalent of 1 hour and 1 minute I want it to display the same: 1:01:00
I achieved this by making the following structure:
public String secondsToTime(int seconds){
String format = "";
int currentMinutes = 0, currentHour = 0;
if((seconds / 60) > 0 ) {
currentMinutes = seconds / 60;
seconds = seconds - currentMinutes * 60;
}
if(currentMinutes >= 60)
{
currentHour = currentMinutes / 60;
currentMinutes = currentMinutes - currentHour * 60;
}
if(currentHour == 0) {
if(currentMinutes < 10 && seconds < 10)
format = "0"+currentMinutes+":0"+seconds;
else if(currentMinutes > 9 && seconds < 10)
format = currentMinutes+":0"+seconds;
else if(currentMinutes > 9 && seconds > 9)
format = currentMinutes+":"+seconds;
else if(currentMinutes < 10 && seconds > 9)
format = "0"+currentMinutes+":"+seconds;
}
else
{
Log.i("TEST", "Current hour este" + currentHour);
if(currentMinutes < 10 && seconds < 10)
format = currentHour+":0"+currentMinutes+":0"+seconds;
else if(currentMinutes > 9 && seconds < 10)
format = currentHour+":"+currentMinutes+":0"+seconds;
else if(currentMinutes > 9 && seconds > 9)
format = currentHour+":"+currentMinutes+":"+seconds;
else if(currentMinutes < 10 && seconds > 9)
format = currentHour+":0"+currentMinutes+":"+seconds;
}
return format;
}
Is there a faster way to do this ?
This questions is not duplicate because the java.util.concurrent.TimeUnit
doesn't follow a standard if you want to show up the format I want.
I agree that he does the transformations for you but I still need many if statements to check everytime if there is an hour or not I can display my minutes with only 1 character and don't display the hour because is irrelevant to have 00 hour.
I am doing this searching and asking those questions because I want to use this algorithm into a media player on Android
to show the total song time and the current second of the song.
For example I have some mixes that have over an hour and music with few minutes, it's irrelevant to show 00:02:30 total time of a music file while playing it, the correct way would be: 2:30 because there is no hour ( hour == 0 ) also if a music file have 2 minutes and 3 seconds it's incorrect to say 2:3, the correct way would be 2:03.