Search in sources :

Example 1 with ReadableDuration

use of org.joda.time.ReadableDuration in project joda-time-android by dlew.

the class DateUtils method formatDuration.

/**
     * Return given duration in a human-friendly format. For example, "4
     * minutes" or "1 second". Returns only largest meaningful unit of time,
     * from seconds up to hours.
     *
     * The longest duration it supports is hours.
     *
     * This method assumes that there are 60 minutes in an hour,
     * 60 seconds in a minute and 1000 milliseconds in a second.
     * All currently supplied chronologies use this definition.
     */
public static CharSequence formatDuration(Context context, ReadableDuration readableDuration) {
    Resources res = context.getResources();
    Duration duration = readableDuration.toDuration();
    final int hours = (int) duration.getStandardHours();
    if (hours != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_hours, hours, hours);
    }
    final int minutes = (int) duration.getStandardMinutes();
    if (minutes != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_minutes, minutes, minutes);
    }
    final int seconds = (int) duration.getStandardSeconds();
    return res.getQuantityString(R.plurals.joda_time_android_duration_seconds, seconds, seconds);
}
Also used : ReadableDuration(org.joda.time.ReadableDuration) Duration(org.joda.time.Duration) Resources(android.content.res.Resources)

Example 2 with ReadableDuration

use of org.joda.time.ReadableDuration in project joda-time by JodaOrg.

the class ReadableDurationConverter method setInto.

//-----------------------------------------------------------------------
/**
     * Extracts duration values from an object of this converter's type, and
     * sets them into the given ReadWritableDuration.
     *
     * @param writablePeriod  period to get modified
     * @param object  the object to convert, must not be null
     * @param chrono  the chronology to use, must not be null
     * @throws NullPointerException if the duration or object is null
     * @throws ClassCastException if the object is an invalid type
     * @throws IllegalArgumentException if the object is invalid
     */
public void setInto(ReadWritablePeriod writablePeriod, Object object, Chronology chrono) {
    ReadableDuration dur = (ReadableDuration) object;
    chrono = DateTimeUtils.getChronology(chrono);
    long duration = dur.getMillis();
    int[] values = chrono.get(writablePeriod, duration);
    for (int i = 0; i < values.length; i++) {
        writablePeriod.setValue(i, values[i]);
    }
}
Also used : ReadableDuration(org.joda.time.ReadableDuration)

Aggregations

ReadableDuration (org.joda.time.ReadableDuration)2 Resources (android.content.res.Resources)1 Duration (org.joda.time.Duration)1