Formatting Durations in Java as H:MM:SS
In Java, the existing time formatting utilities are primarily focused on formatting timestamps rather than durations. To format durations effectively, you can employ a straightforward approach using a Formatter or related shortcuts.
For instance, given an integer representing seconds (s), you can format it into the desired H:MM:SS format using the following pattern:
String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60));
In this expression:
By utilizing this approach, you can conveniently format durations into the H:MM:SS pattern without introducing external libraries into your Java code.
The above is the detailed content of How Can I Format a Duration in Java as H:MM:SS?. For more information, please follow other related articles on the PHP Chinese website!