To format a duration in Java, you can use the Duration class from the java.time package, which is part of the Java Date and Time API introduced in Java 8 and later. Here's how you can format a duration as "H:MM:SS":
import java.time.Duration; public class FormatDuration { public static void main(String[] args) { Duration duration = Duration.ofHours(3).plusMinutes(15).plusSeconds(30); String formattedDuration = formatDuration(duration); System.out.println("Formatted Duration: " + formattedDuration); } public static String formatDuration(Duration duration) { long hours = duration.toHours(); long minutes = duration.toMinutesPart(); long seconds = duration.toSecondsPart(); return String.format("%02d:%02d:%02d", hours, minutes, seconds); } } In this example:
We create a Duration object representing 3 hours, 15 minutes, and 30 seconds.
We define a formatDuration method that takes a Duration as an argument and formats it as "H:MM:SS" using String.format.
Inside the formatDuration method, we use the toHours(), toMinutesPart(), and toSecondsPart() methods of the Duration class to extract the hours, minutes, and seconds components.
We use String.format to format the duration as "H:MM:SS," ensuring that each component is zero-padded to two digits.
Finally, we print the formatted duration to the console.
When you run this code, it will display the formatted duration as "03:15:30" based on the Duration object provided. You can adjust the Duration object to represent the desired duration you want to format.
firebaseui angular-http pdflatex android-navigation outlook-redemption metadata gnu-findutils jexcelapi positional-argument microservices