Java Utililty Methods Duration Format

List of utility methods to do Duration Format

Description

The list of methods to do Duration Format are organized into topic(s).

Method

StringdurationToString(Long ms)
duration To String
if (ms == null) {
    return null;
StringBuffer sb = new StringBuffer();
long days = ms / 86400000l;
ms = ms % 86400000l;
if (days < 10)
    sb.append("0");
...
StringdurationToString(long x)
duration To String
String suffix = "";
if (x % 1000 == 0) {
    x /= 1000;
    suffix = "s";
if (x % 60 == 0) {
    x /= 60;
    suffix = "m";
...
StringdurationTranslator(String sonarDuration)
duration Translator
String daysPart = "P"
        + (sonarDuration.contains("d") ? sonarDuration.substring(0, sonarDuration.indexOf('d')) + "D" : "");
String timePart = (sonarDuration.contains("d") ? sonarDuration.substring(sonarDuration.indexOf('d') + 1)
        : sonarDuration);
return daysPart + (timePart.isEmpty() ? "" : "T" + timePart.replaceAll("min", "M").replaceAll("h", "H"));
Stringformat8601Duration(long duration)
Format an ISO 8601 Duration from the number of milliseconds
int msPerHour = (1000 * 60 * 60);
int hours = (int) Math.floor(duration / msPerHour);
long durationRemaining = duration % msPerHour;
int msPerMin = (60 * 1000);
int mins = (int) Math.floor(durationRemaining / msPerMin);
durationRemaining = durationRemaining % msPerMin;
int msPerS = 1000;
int secs = (int) Math.floor(durationRemaining / msPerS);
...
StringformatBuildDuration(final long duration)
Takes a duration and formats it as h:m:s:ms.
final StringBuilder formattedDuration = new StringBuilder();
final int hours = (int) Math.floor(duration / (MILLISECONDS * MINUTES * SECONDS));
final int minutes = (int) Math
        .floor(duration % (MILLISECONDS * MINUTES * SECONDS) / (MILLISECONDS * MINUTES));
final int seconds = (int) Math
        .floor(duration % (MILLISECONDS * MINUTES * SECONDS) % (MILLISECONDS * MINUTES) / MILLISECONDS);
boolean hoursAdded = false;
if (hours != 0) {
...
StringformatDuration(double dblSeconds)
format Duration
int nMinutes = (int) (dblSeconds / 60);
int nSeconds = (int) dblSeconds - (nMinutes * 60);
int nHundredths = (int) ((dblSeconds - (nMinutes * 60) - nSeconds) * 100);
return String.valueOf(nMinutes) + ":"
        + (nSeconds <= 9 ? ("0" + String.valueOf(nSeconds)) : String.valueOf(nSeconds)) + "."
        + (nHundredths <= 9 ? ("0" + String.valueOf(nHundredths)) : String.valueOf(nHundredths));
StringformatDuration(Duration d)
format Duration
long s = d.getSeconds();
return String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60);
StringformatDuration(Duration duration)
format Duration
return String.format("%02d:%02d:%02d", duration.toHours(), duration.getSeconds() % 3600 / 60,
        duration.getSeconds() % 60);
StringformatDuration(Duration duration)
Returns a formated string representation of the Duration object.
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;
StringformatDuration(Duration duration)
Formats a Duration.
long seconds = duration.getSeconds();
long absSeconds = Math.abs(seconds);
String positive = String.format("%02d:%02d",
        (absSeconds ) / 60, absSeconds % 60);
return seconds < 0 ? "-" + positive : positive;