1

I'm aware that you can use DateUtils.formatElapsedTime(seconds) to convert a number of seconds into a String with the format HH:MM:SS. But are there any utility functions that let me perform the same conversion but without the seconds?

For example, I want to convert 3665 seconds into 1:01, even though it's exactly 1:01:05. In other words, simply dropping the seconds part.

Would strongly prefer an answer that points to a utility function (if one exists) rather than a bunch of home rolled algorithms.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • 1
    Make use of `java.time.Duration` – MadProgrammer Apr 30 '18 at 07:55
  • I believe the utility function exists in [Time4J, the advanced date, time, zone and iInterval library for Java](http://time4j.net). – Ole V.V. Apr 30 '18 at 08:19
  • I believe you can find a lot of inspiration from searching. For just one exampe there’s a near-duplicate here: [How to format a duration in java? (e.g format H:MM:SS)](https://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss). With an answer using the Time4J library I mentioned. – Ole V.V. Apr 30 '18 at 08:30

3 Answers3

4

Use Apache Commons Lang

You could use utility class DateFormatUtils of Apache's well known utility library Commons Lang, combined with TimeUnit to convert from seconds to milliseconds:

static String format(long durationSeconds) {
    long durationMillis = TimeUnit.SECONDS.toMillis(durationSeconds);
    // Commons lang:
    return DurationFormatUtils.formatDuration(durationMillis, "HH:mm");
}

Use with input 3665 it prints:

01:01

Personally I'd prefer to use Java8 or Java9 standard library (see the other answers) rather than introducing a dependency just to make it 1 method call.

Jens Hoffmann
  • 6,699
  • 2
  • 25
  • 31
3

MadProgrammer has already provided a good Java 8 answer (which will work in Java 6 and 7 too when you use the ThreeTen Backport). In Java 9 still a bit more of the calculation can be done in the library:

    int seconds = 3665;
    Duration dur = Duration.ofSeconds(seconds);
    String formatted = String.format("%d:%02d", dur.toHours(), dur.toMinutesPart());
    System.out.println(formatted);

Output:

1:01

The toMinutesPart method and other toXxxPart methods were introduced in Java 9.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
2

Based on the available information, you seem to be wanting to format a duration based value. Lucky for us, since Java 8, there is now a new java.time API which includes a Duration class.

Unfortunately, it doesn't (at least the last time checked) support a formatter for it.

However, you could easily roll your own...

protected static String format(Duration duration) {
    long hours = duration.toHours();
    long mins = duration.minusHours(hours).toMinutes();
    return String.format("%02d:%02d", hours, mins);
}

Which when used with something like...

System.out.println(format(Duration.ofSeconds(3665)));

prints out 01:01.

Now I know you'd "prefer" utility methods, but you're unlikely to find something that fits your "every" need and this at least gives you a starting point. Besides, you could always make a pull request ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • No bunch of home rolled algorithms here. The calculations take place in the library. I agree that this is advantageous. – Ole V.V. Apr 30 '18 at 08:23
  • 1
    This is great, especially using `String.format()` to format the output. The answers I found while searching were mostly just int-division, modulus and concatenating strings, resulting in loss of leading zeros. That's what I meant by "home rolled algorithms". – Magnus Apr 30 '18 at 09:21
  • 1
    @BadCash Oh dear lord no - if there is one thing that will attract my downvote - it's date/time arithmetic - There is no excuse for it, especially these days – MadProgrammer May 01 '18 at 00:36