1

I have a Java function that convert seconds to an specific format (hh:mm:ss):

public static String formatChronometer(long seconds) {
    return String.format("%02d:%02d:%02d", TimeUnit.SECONDS.toHours(seconds),
            TimeUnit.SECONDS.toMinutes(seconds) % TimeUnit.HOURS.toMinutes(1),
            TimeUnit.SECONDS.toSeconds(seconds) % TimeUnit.MINUTES.toSeconds(1));
}

My client want to show this chronometer without the "hour" labels. Example:

  1. if 100 hours => 6000:00
  2. if 0 hours and 50 minutes => 50:00
  3. if 1 hour and 12 minutes and 30 seconds => 72:30
  4. if 10 hours and 5 minutes => 600:05
  5. if 0 hours, 0 minutes and 0 seconds => 00:00

How should I change my method?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
aseolin
  • 1,184
  • 3
  • 17
  • 35

4 Answers4

3

Just ditch the hours bit.

return String.format("%04d:%02d",
    TimeUnit.SECONDS.toMinutes(seconds),
    TimeUnit.SECONDS.toSeconds(seconds) % TimeUnit.MINUTES.toSeconds(1));
Him
  • 5,257
  • 3
  • 26
  • 83
3

For flexible minutes number you have to use %d instead of %02d which specify how many digit you want, your solution should look like this :

return String.format("%d:%02d",
        TimeUnit.SECONDS.toMinutes(seconds),
        TimeUnit.SECONDS.toSeconds(seconds) % TimeUnit.MINUTES.toSeconds(1)
);

Example

long[] times = {360000, 3000, 4350, 36300, 0};
for (long time : times) {
    String result = String.format("%d:%02d",
            TimeUnit.SECONDS.toMinutes(time),
            TimeUnit.SECONDS.toSeconds(time) % TimeUnit.MINUTES.toSeconds(1));
    System.out.println(String.format("%d : %s", time, result));
}

Outputs

360000 : 6000:00
3000   : 50:00
4350   : 72:30
36300  : 605:00
0      : 0:00
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2
public static String formatChronometer(long seconds) {
    Duration dur = Duration.ofSeconds(seconds);
    return String.format("%02d:%02d", dur.toMinutes(), dur.toSecondsPart());
}

When I fed 36005 into this method, it returned 600:05.

I take it that your chronometer seconds denote a duration, an amount of time. Either an elapsed time or a value to count down from. Not a time of day (you mentioned 100 hours an example, but the time of day is never 100 hours). In that case I would clearly use the Duration class for modelling it. It’s the most correct thing to do. Some use the word self-documenting about code written with such a consideration in mind.

The Duration.toSecondsPart method was only introduced in Java 9. If you are using Java 8 (or Java 6 or 7 with the ThreeTen Backport), get the seconds part by subtracting the minutes and then converting to seconds:

    long minutes = dur.toMinutes();
    dur = dur.minusMinutes(minutes);
    return String.format("%02d:%02d", minutes, dur.toSeconds());

Then the result is the same.

PS If your seconds denoted a time-of-day instead, the correct and self-documenting solution would be to use the LocalTime class:

    LocalTime chrono = LocalTime.ofSecondOfDay(seconds);
    return String.format("%02d:%02d",
            chrono.get(ChronoField.MINUTE_OF_DAY), chrono.getSecond());

This will reject seconds values greater than 86399 with an exception since the time of day cannot be 24:00 or greater.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Remove your hours from the formatting

public static String formatChronometer(long seconds) {
    return String.format("%02d:%02d",
            TimeUnit.SECONDS.toMinutes(seconds),
            TimeUnit.SECONDS.toSeconds(seconds) % TimeUnit.MINUTES.toSeconds(1));
}
Daniel Taub
  • 5,133
  • 7
  • 42
  • 72