Example usage for org.joda.time.format PeriodFormatterBuilder toFormatter

List of usage examples for org.joda.time.format PeriodFormatterBuilder toFormatter

Introduction

In this page you can find the example usage for org.joda.time.format PeriodFormatterBuilder toFormatter.

Prototype

public PeriodFormatter toFormatter() 

Source Link

Document

Constructs a PeriodFormatter using all the appended elements.

Usage

From source file:ch.eitchnet.android.util.JodaHelper.java

License:Open Source License

public static String toHourMinute(Duration duration) {
    PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
    builder.printZeroAlways();//ww w.  j  a va  2  s . c o m
    if (duration.isShorterThan(Duration.ZERO))
        builder.appendLiteral("-");
    builder.minimumPrintedDigits(2).appendHours().appendLiteral(":").minimumPrintedDigits(2).appendMinutes();
    return builder.toFormatter().print(new Period(Math.abs(duration.getMillis())));
}

From source file:com.mobileman.kuravis.core.util.DateTimeUtils.java

License:Apache License

/**
 * @param date/*from  w  w  w  .j a v a2s.  c  o  m*/
 * @return formatted elapsed time from now
 */
public static String fmtElapsedTime(Date date) {
    if (date == null) {
        return "";
    }
    Period period = new Period(date.getTime(), Calendar.getInstance().getTimeInMillis());
    PeriodFormatterBuilder pf = new PeriodFormatterBuilder();
    pf.appendPrefix(" vor ");
    if (period.getYears() > 0) {
        pf.appendYears().appendSuffix(" Jahr", " Jahren");
    } else if (period.getMonths() > 0) {
        pf.appendMonths().appendSuffix(" Monat", " Monaten");
    } else if (period.getWeeks() > 0) {
        pf.appendWeeks().appendSuffix(" Woche ", " Wochen");
    } else if (period.getDays() > 0) {
        pf.appendDays().appendSuffix(" Tag ", " Tagen");
    } else if (period.getHours() > 0) {
        pf.appendHours().appendSuffix(" Stunde ", " Stunden");
    } else if (period.getMinutes() > 0) {
        pf.appendMinutes().appendSuffix(" Minute ", " Minuten");
    } else if (period.getSeconds() > 0) {
        pf.appendSeconds().appendSuffix(" Sekunde ", " Sekunden");
    }
    return pf.toFormatter().print(period);
}

From source file:org.jspresso.framework.util.format.DurationFormatter.java

License:Open Source License

/**
 * Constructs a new {@code DurationFormatter} instance.
 *
 * @param translationProvider/*from  www.j  ava  2 s  .c  o  m*/
 *          the translation provider for duration labels.
 * @param locale
 *          the locale the formatter must be constructed in.
 */
public DurationFormatter(ITranslationProvider translationProvider, Locale locale, boolean secondsAware,
        boolean millisecondsAware) {
    super();
    PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
    builder.appendDays();
    builder.appendSuffix(" " + translationProvider.getTranslation("day", locale),
            " " + translationProvider.getTranslation("days", locale));
    builder.appendSeparator(" ");
    builder.appendHours();
    builder.appendSuffix(" " + translationProvider.getTranslation("hour", locale),
            " " + translationProvider.getTranslation("hours", locale));
    builder.appendSeparator(" ");
    builder.appendMinutes();
    builder.appendSuffix(" " + translationProvider.getTranslation("minute", locale),
            " " + translationProvider.getTranslation("minutes", locale));
    if (secondsAware) {
        builder.appendSeconds();
        builder.appendSuffix(" " + translationProvider.getTranslation("second", locale),
                " " + translationProvider.getTranslation("seconds", locale));
    }
    if (millisecondsAware) {
        builder.appendMillis();
        builder.appendSuffix(" " + translationProvider.getTranslation("millisecond", locale),
                " " + translationProvider.getTranslation("milliseconds", locale));
    }
    this.formatter = builder.toFormatter().withLocale(locale);
}

From source file:org.openvpms.archetype.i18n.time.DateDurationFormatter.java

License:Open Source License

/**
 * Constructs a {@link DateDurationFormatter}.
 *
 * @param showYears   determines if years should be displayed
 * @param showMonths  determines if months should be displayed
 * @param showWeeks   determines if weeks should be displayed
 * @param showDays    determines if days should be displayed
 * @param showHours   determines if hours should be displayed
 * @param showMinutes determines if minutes should be displayed
 *///from  ww w. j  a  v  a2 s  .  c  o  m
protected DateDurationFormatter(boolean showYears, boolean showMonths, boolean showWeeks, boolean showDays,
        boolean showHours, boolean showMinutes) {
    this.showYears = showYears;
    this.showMonths = showMonths;
    this.showWeeks = showWeeks;
    this.showDays = showDays;
    this.showHours = showHours;
    this.showMinutes = showMinutes;

    PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
    if (showYears) {
        builder = builder.appendYears().appendSuffix(YEAR_SUFFIX, YEARS_SUFFIX).appendSeparator(" ");
    }
    if (showMonths) {
        builder = builder.appendMonths().appendSuffix(MONTH_SUFFIX, MONTHS_SUFFIX).appendSeparator(" ");
    }
    if (showWeeks) {
        builder = builder.appendWeeks().appendSuffix(WEEK_SUFFIX, WEEKS_SUFFIX).appendSeparator(" ");
    }
    if (showDays) {
        builder = builder.appendDays().appendSuffix(DAY_SUFFIX, DAYS_SUFFIX).appendSeparator(" ");
    }
    if (showHours) {
        builder = builder.appendHours().appendSuffix(HOUR_SUFFIX, HOURS_SUFFIX).appendSeparator(" ");
    }
    if (showMinutes) {
        builder = builder.appendMinutes().appendSuffix(MINUTE_SUFFIX, MINUTES_SUFFIX).appendSeparator(" ");
    }

    formatter = builder.toFormatter();
}

From source file:org.starnub.utilities.time.DateAndTimes.java

License:Open Source License

public static String getPeriodFormattedFromMilliseconds(long duration, boolean printZeros, int digitCount,
        String... separators) {//w  ww  .  j  a  va  2  s.  c om
    Period period = new Period(duration).normalizedStandard();
    PeriodFormatterBuilder formatterBuilder = new PeriodFormatterBuilder().minimumPrintedDigits(digitCount);
    if (printZeros) {
        formatterBuilder.printZeroIfSupported();
    }
    formatterBuilder.appendYears().appendSeparator(separators[0]).appendMonths().appendSeparator(separators[1])
            .appendWeeks().appendSeparator(separators[2]).appendDays().appendSeparator(separators[3])
            .appendHours().appendSeparator(separators[4]).appendMinutes().appendSeparator(separators[5])
            .appendSeconds().appendLiteral(separators[6]);
    return formatterBuilder.toFormatter().print(period);
}

From source file:org.starnub.utilities.time.DateAndTimes.java

License:Open Source License

public static String getPeriodFormattedFromMillisecondsSuffix(long duration, boolean printZeros, int digitCount,
        String... separators) {//from  w  ww  .jav a  2  s . c  om
    Period period = new Period(duration).normalizedStandard();
    PeriodFormatterBuilder formatterBuilder = new PeriodFormatterBuilder().minimumPrintedDigits(digitCount);
    if (printZeros) {
        formatterBuilder.printZeroIfSupported();
    }
    formatterBuilder.appendYears().appendSuffix(separators[0], separators[1]).appendMonths()
            .appendSuffix(separators[2], separators[3]).appendWeeks().appendSuffix(separators[4], separators[5])
            .appendDays().appendSuffix(separators[6], separators[7]).appendHours()
            .appendSuffix(separators[8], separators[9]).appendMinutes()
            .appendSuffix(separators[10], separators[11]).appendSeconds()
            .appendSuffix(separators[12], separators[13]);
    return formatterBuilder.toFormatter().print(period);
}