Java intelligently converts seconds into time

I want to create a library because I can't find one to convert seconds or milliseconds into time Well, I mean:

1) If I have 61 seconds, the time format is: 1:01 (not 1:1)

2) If I'm equivalent to 1 hour and 1 minute, I want it to display the same: 1:01:00

I achieved this through 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?

This question is not repeated, because if you want to display the format I want, Java util. concurrent. Timeunit does not follow the standard I agree with him to do the conversion for you, but I still need a lot of if statements to check whether there is an hour at a time. I can't display my minute, only 1 character and don't display the hour, because it has nothing to do with 00 hours

I'm doing this search and asking these questions because I want to use this algorithm for media players on Android to display the total song time and the second part of the current song

For example, I have some mixing for more than one hour and several minutes of music, which has nothing to do with the total time of playing music files at 00:02:30. The correct way is: 2:30 because there is no time (hour = = 0). If the music file has 2 minutes and 3 seconds, say that 2:3 is incorrect. The correct way is 2:03

Solution

Just convert it to a string and cut off the leading character, as long as it is "0" or ":

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String myDate = dateFormat.format(new  Date(TimeUnit.SECONDS.toMillis(seconds)));
while (( myDate.charAt(0).equals("0") || myDate.charAt(0).equals(":")){
    myDate = myDate.substring(1);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>