This API is very basic and is intended to be a building block for more fancy APIs. The caller tells it exactly what to display in a locale independent way. While this class automatically provides the correct plural forms, the grammatical form is otherwise as neutral as possible. It is the caller's responsibility to handle cut-off logic such as deciding between displaying "in 7 days" or "in 1 week." This API supports relative dates involving one single unit. This API does not support relative dates involving compound units. e.g "in 5 days and 4 hours" nor does it support parsing. This class is both immutable and thread-safe.
Here are some examples of use:
RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(); fmt.format(1, Direction.NEXT, RelativeUnit.DAYS); // "in 1 day" fmt.format(3, Direction.NEXT, RelativeUnit.DAYS); // "in 3 days" fmt.format(3.2, Direction.LAST, RelativeUnit.YEARS); // "3.2 years ago" fmt.format(Direction.LAST, AbsoluteUnit.SUNDAY); // "last Sunday" fmt.format(Direction.THIS, AbsoluteUnit.SUNDAY); // "this Sunday" fmt.format(Direction.NEXT, AbsoluteUnit.SUNDAY); // "next Sunday" fmt.format(Direction.PLAIN, AbsoluteUnit.SUNDAY); // "Sunday" fmt.format(Direction.LAST, AbsoluteUnit.DAY); // "yesterday" fmt.format(Direction.THIS, AbsoluteUnit.DAY); // "today" fmt.format(Direction.NEXT, AbsoluteUnit.DAY); // "tomorrow" fmt.format(Direction.PLAIN, AbsoluteUnit.NOW); // "now"
In the future, we may add more forms, such as abbreviated/short forms (3 secs ago), and relative day periods ("yesterday afternoon"), etc.
| Modifier and Type | Class and Description |
|---|---|
| public static enum | RelativeDateTimeFormatter.AbsoluteUnit
Represents an absolute unit. |
| private static class | |
| public static enum | RelativeDateTimeFormatter.Direction
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday" |
| public static class | RelativeDateTimeFormatter.Field
Field constants used when accessing field information for relative datetime strings in FormattedValue. |
| public static class | RelativeDateTimeFormatter.FormattedRelativeDateTime
Represents the result of a formatting operation of a relative datetime. |
| private static class | |
| private static class | |
| public static enum | RelativeDateTimeFormatter.RelativeDateTimeUnit
Represents the unit for formatting a relative date. |
| public static enum | RelativeDateTimeFormatter.RelativeUnit
Represents the unit for formatting a relative date. |
| private static class | RelativeDateTimeFormatter.RelDateTimeDataSink
Sink for enumerating all of the relative data time formatter names. |
| public static enum | RelativeDateTimeFormatter.Style
The formatting style |
| Modifier and Type | Field and Description |
|---|---|
| private final BreakIterator | |
| private static final RelativeDateTimeFormatter. | |
| private final DisplayContext | |
| private final String | |
| private final DateFormatSymbols | |
| private static final RelativeDateTimeFormatter. | |
| private final ULocale | |
| private final NumberFormat | |
| private final EnumMap | |
| private final PluralRules | |
| private final EnumMap | |
| private final RelativeDateTimeFormatter. | |
| private int[] |
| Access | Constructor and Description |
|---|---|
| private | RelativeDateTimeFormatter(EnumMap<RelativeDateTimeFormatter.
|
| Modifier and Type | Method and Description |
|---|---|
| private String | |
| private void | |
| public String | Returns: the date and time concatenated according to the default calendar in this locale e.g 'yesterday, 3:45'the relative date e.g 'yesterday' relativeDateString, String the time e.g '3:45' timeString)Combines a relative date string and a time string in this object's locale. |
| public String | Returns: the formatted stringThe numerical amount e.g 5. This value is formatted
according to this object's quantity, RelativeDateTimeFormatter.NumberFormat object.NEXT means a future relative date; LAST means a past
relative date. direction,the unit e.g day? month? year? unitFormats a relative date with a quantity such as "in 5 days" or "3 months ago". |
| public String | Returns: the formatted string. If direction has a value that is documented as not being fully supported in every locale (for example NEXT_2 or LAST_2) then this function may return null to signal that no formatted string is available.NEXT, LAST, THIS, etc. direction,e.g SATURDAY, DAY, MONTH unitFormats a relative date without a quantity. |
| public String | Returns: The formatted string (may be empty in case of error)The signed offset for the specified field. offset, RelativeDateTimeFormatter.The unit to use when formatting the relative
date, e.g. RelativeDateTimeUnit.WEEK,
RelativeDateTimeUnit.FRIDAY. unitFormat a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g. "last week", "this week", "next week", "yesterday", "tomorrow". |
| private String | formatAbsoluteImpl(RelativeDateTimeFormatter.
Implementation method for format and formatToValue with AbsoluteUnit |
| private FormattedStringBuilder | formatImpl(double quantity, RelativeDateTimeFormatter.
Implementation method for format and formatToValue with RelativeUnit |
| public String | Returns: The formatted string (may be empty in case of error)The signed offset for the specified unit. This
will be formatted according to this object's
NumberFormat object. offset, RelativeDateTimeFormatter.The unit to use when formatting the relative
date, e.g. RelativeDateTimeUnit.WEEK,
RelativeDateTimeUnit.FRIDAY. unitFormat a combination of RelativeDateTimeUnit and numeric offset using a numeric style, e.g. "1 week ago", "in 1 week", "5 weeks ago", "in 5 weeks". |
| private FormattedStringBuilder | formatNumericImpl(double offset, RelativeDateTimeFormatter.
Implementation method for formatNumeric and formatNumericToValue |
| public RelativeDateTimeFormatter. | Returns: The formatted string (may be empty in case of error)The signed offset for the specified unit. This
will be formatted according to this object's
NumberFormat object. offset, RelativeDateTimeFormatter.The unit to use when formatting the relative
date, e.g. RelativeDateTimeUnit.WEEK,
RelativeDateTimeUnit.FRIDAY. unitFormat a combination of RelativeDateTimeUnit and numeric offset using a numeric style, e.g. "1 week ago", "in 1 week", "5 weeks ago", "in 5 weeks". |
| private CharSequence | formatRelativeImpl(double offset, RelativeDateTimeFormatter.
Implementation method for format and formatToValue with RelativeDateTimeUnit. |
| public RelativeDateTimeFormatter. | Returns: the formatted relative datetimeThe numerical amount e.g 5. This value is formatted
according to this object's quantity, RelativeDateTimeFormatter.NumberFormat object.NEXT means a future relative date; LAST means a past
relative date. direction,the unit e.g day? month? year? unitFormats a relative date with a quantity such as "in 5 days" or "3 months ago". |
| public RelativeDateTimeFormatter. | Returns: the formatted string. If direction has a value that is documented as not being fully supported in every locale (for example NEXT_2 or LAST_2) then this function may return null to signal that no formatted string is available.NEXT, LAST, THIS, etc. direction,e.g SATURDAY, DAY, MONTH unitFormats a relative date without a quantity. |
| public RelativeDateTimeFormatter. | Returns: The formatted string (may be empty in case of error)The signed offset for the specified field. offset, RelativeDateTimeFormatter.The unit to use when formatting the relative
date, e.g. RelativeDateTimeUnit.WEEK,
RelativeDateTimeUnit.FRIDAY. unitFormat a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g. "last week", "this week", "next week", "yesterday", "tomorrow". |
| private String | getAbsoluteUnitString(RelativeDateTimeFormatter.
Gets the string value from qualitativeUnitMap with fallback based on style. |
| public DisplayContext | |
| public RelativeDateTimeFormatter. | |
| public static RelativeDateTimeFormatter | |
| public static RelativeDateTimeFormatter | Returns: An instance of RelativeDateTimeFormatter.the locale. locale)Returns a RelativeDateTimeFormatter for a particular locale. |
| public static RelativeDateTimeFormatter | Returns: An instance of RelativeDateTimeFormatter.the locale)java..Returns a RelativeDateTimeFormatter for a particular |
| public static RelativeDateTimeFormatter | Returns: An instance of RelativeDateTimeFormatter.the locale locale, NumberFormat the number format object. It is defensively copied to ensure thread-safety
and immutability of this class. nf)Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object. |
| public static RelativeDateTimeFormatter | getInstance(ULocale
the locale locale, NumberFormat the number format object. It is defensively copied to ensure thread-safety
and immutability of this class. May be null. nf, RelativeDateTimeFormatter.the style. style,the capitalization context. capitalizationContext)Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object, style, and capitalization context |
| public static RelativeDateTimeFormatter | Returns: An instance of RelativeDateTimeFormatter.the locale, NumberFormat java.the number format object. It is defensively copied to ensure thread-safety
and immutability of this class. nf)Returns a RelativeDateTimeFormatter for a particular |
| public NumberFormat | Returns: A copy of the NumberFormat.Returns a copy of the NumberFormat this object is using. |
| private String | getRelativeUnitPattern(RelativeDateTimeFormatter.
|
| private String | getRelativeUnitPluralPattern(RelativeDateTimeFormatter.
|
| private static RelativeDateTimeFormatter. |