Java converts seconds to time

I want to create a library because I cannot find one to convert seconds or milliseconds to time. Over time, I mean:

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

2) If I have the equivalent of 1 hour and 1 minute, I want it to show the same: 1:01:00

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

These questions are not repeated because it is java.util.concurrent.TimeUnit

not standard if you want to display the format that I want. I agree that it does the conversions for you, but I still need a lot of if statements to check every time if there is an hour or not. I can display my minutes with only 1 character and not display the hour because it doesn't matter to have 00 hour.

I am doing this search and asking these questions because I want to use this algorithm in the media player on Android

to show the total song time and the current second song.

For example, I have several mixes that have more than an hour and music with a few minutes, it doesn't matter to show the total playing time of the music file 00:02:30, the correct way is 2:30 because there is no hour (hour == 0), also if the music file has 2 minutes and 3 seconds, it is not correct to say 2: 3, the correct path is 2:03.

+3


source to share


3 answers


Just convert it to String and strip off leading characters if they are "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);
}

      

+1


source


How about this if I understood correctly:



   final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
   return dateFormat.format(new Date(TimeUnit.SECONDS.toMillis(seconds)));

      

+7


source


How about this:

/**
 * @author Alexey Belov
 */
public class DateFormatTest {

    private String format(int seconds) {
        final DateFormat dateFormat;
        if (seconds < 60) {
            dateFormat = new SimpleDateFormat("ss");
        } else if (seconds < 60 * 60) {
            dateFormat = new SimpleDateFormat("m:ss");
        } else {
            dateFormat = new SimpleDateFormat("H:mm:ss");
        }
        final Calendar gmt = Calendar.getInstance(Locale.ENGLISH);
        gmt.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
        gmt.add(Calendar.SECOND, seconds);
        return dateFormat.format(gmt.getTime());
    }

    @Test
    public void test10100() {
        Assert.assertEquals("1:01:00", format(60 * 60 + 60));
    }

    @Test
    public void test101() {
        Assert.assertEquals("1:01", format(61));
    }

    @Test
    public void test01() {
        Assert.assertEquals("01", format(1));
    }

    @Test
    public void test120000() {
        Assert.assertEquals("12:00:00", format(60 * 60 * 12));
    }


}

      

+3


source







All Articles