Search in sources :

Example 1 with DontCareFieldPosition

use of android.icu.impl.DontCareFieldPosition in project j2objc by google.

the class RelativeDateTimeFormatter method format.

/**
 * Formats a relative date with a quantity such as "in 5 days" or
 * "3 months ago"
 * @param quantity The numerical amount e.g 5. This value is formatted
 * according to this object's {@link NumberFormat} object.
 * @param direction NEXT means a future relative date; LAST means a past
 * relative date.
 * @param unit the unit e.g day? month? year?
 * @return the formatted string
 * @throws IllegalArgumentException if direction is something other than
 * NEXT or LAST.
 */
public String format(double quantity, Direction direction, RelativeUnit unit) {
    if (direction != Direction.LAST && direction != Direction.NEXT) {
        throw new IllegalArgumentException("direction must be NEXT or LAST");
    }
    String result;
    int pastFutureIndex = (direction == Direction.NEXT ? 1 : 0);
    // class we must guarantee that only one thread at a time uses our numberFormat.
    synchronized (numberFormat) {
        StringBuffer formatStr = new StringBuffer();
        DontCareFieldPosition fieldPosition = DontCareFieldPosition.INSTANCE;
        StandardPlural pluralForm = QuantityFormatter.selectPlural(quantity, numberFormat, pluralRules, formatStr, fieldPosition);
        String formatter = getRelativeUnitPluralPattern(style, unit, pastFutureIndex, pluralForm);
        result = SimpleFormatterImpl.formatCompiledPattern(formatter, formatStr);
    }
    return adjustForContext(result);
}
Also used : StandardPlural(android.icu.impl.StandardPlural) DontCareFieldPosition(android.icu.impl.DontCareFieldPosition)

Aggregations

DontCareFieldPosition (android.icu.impl.DontCareFieldPosition)1 StandardPlural (android.icu.impl.StandardPlural)1