Example usage for org.apache.commons.lang3.time DurationFormatUtils formatPeriod

List of usage examples for org.apache.commons.lang3.time DurationFormatUtils formatPeriod

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time DurationFormatUtils formatPeriod.

Prototype

public static String formatPeriod(final long startMillis, final long endMillis, final String format,
        final boolean padWithZeros, final TimeZone timezone) 

Source Link

Document

Formats the time gap as a string, using the specified format.

Usage

From source file:org.eclipse.hawkbit.ui.utils.SPDateTimeUtil.java

/**
 * Creates a formatted string of a duration in format '1 year 2 months 3
 * days 4 hours 5 minutes 6 seconds' zero values will be ignored in the
 * formatted string.//from   ww  w.  j  a va2 s  .c o  m
 *
 * @param startMillis
 *            the start milliseconds of the duration
 * @param endMillis
 *            the end milliseconds of the duration
 * @param i18N
 *            the i18n service to determine the correct string for e.g.
 *            'year'
 * @return a formatted string for duration label
 */
public static String getDurationFormattedString(final long startMillis, final long endMillis,
        final VaadinMessageSource i18N) {
    final String formatDuration = DurationFormatUtils.formatPeriod(startMillis, endMillis, DURATION_FORMAT,
            false, getBrowserTimeZone());

    final StringBuilder formattedDuration = new StringBuilder();
    final String[] split = formatDuration.split(",");

    for (int index = 0; index < split.length; index++) {
        if (index != 0 && formattedDuration.length() > 0) {
            formattedDuration.append(' ');
        }
        final int value = Integer.parseInt(split[index]);
        if (value != 0) {
            final String suffix = (value == 1) ? i18N.getMessage(DURATION_I18N.get(index).getSingle())
                    : i18N.getMessage(DURATION_I18N.get(index).getPlural());
            formattedDuration.append(value).append(' ').append(suffix);
        }

    }
    return formattedDuration.toString();

}