3

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.

Marian Pavel
  • 2,726
  • 8
  • 29
  • 65
  • possible duplicate of [How to convert Milliseconds to "X mins, x seconds" in Java?](http://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java) – sinclair Jul 02 '15 at 11:09
  • I already check that solution @sinclair , if I convert 61 minutes to hour and then take the minute I will have 1 hour and 1 minute, I want something with this format: 1:01 without forcing it, as you can see I have many if's to do this format. – Marian Pavel Jul 02 '15 at 11:19

3 Answers3

7

How about this, if i understood correctly:

   final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
   return dateFormat.format(new Date(TimeUnit.SECONDS.toMillis(seconds)));
Alex Belov
  • 111
  • 4
  • The only question I have and I doubt about it is if I input 110 seconds I will get 1:50 ? There is incorrect to show 01:50 or 00:01:50 – Marian Pavel Jul 02 '15 at 11:20
4

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));
    }


}
Alex Belov
  • 111
  • 4
2

Simply convert it to String and cut off the leading characters as long as it's "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);
}
sinclair
  • 2,812
  • 4
  • 24
  • 53
  • This is the answer I was looking for, I will do some tests with my algorithm and this later and I will post the results. – Marian Pavel Jul 02 '15 at 11:52