Here you can find the source of formatDuration(Duration duration)
Parameter | Description |
---|---|
duration | the Duration object to format |
public static String formatDuration(Duration duration)
//package com.java2s; //License from project: Open Source License import java.time.Duration; public class Main { /**//from w ww .java 2 s . c o m * Returns a formated string representation of the {@code Duration} object. * Format: HH:MM:SS. * * @see "http://stackoverflow.com/questions/266825/how-to-format-a-duration- * in-java-e-g-format-hmmss" * @param duration * the {@code Duration} object to format * @return a formated string representation of {@code duration} */ public static String formatDuration(Duration duration) { long seconds = duration.getSeconds(); long absSeconds = Math.abs(seconds); String positive = String.format("%d:%02d:%02d", absSeconds / 3600, (absSeconds % 3600) / 60, absSeconds % 60); return seconds < 0 ? "-" + positive : positive; } /** * Returns a formated string representation of {@code seconds}. Format: * HH:MM:SS. * * @param seconds * the duration to format in seconds * @return a formated string representation of {@code seconds} */ public static String formatDuration(long seconds) { long absSeconds = Math.abs(seconds); String positive = String.format("%d:%02d:%02d", absSeconds / 3600, (absSeconds % 3600) / 60, absSeconds % 60); return seconds < 0 ? "-" + positive : positive; } }