RelativeDateTimeFormatter
class RelativeDateTimeFormatter
kotlin.Any | |
↳ | android.icu.text.RelativeDateTimeFormatter |
Formats simple relative dates. There are two types of relative dates that it handles:
- relative dates with a quantity e.g "in 5 days"
- relative dates without a quantity e.g "next Tuesday"
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.
Summary
Nested classes | |
---|---|
Represents an absolute unit. |
|
Represents a direction for an absolute unit e. |
|
Represents the result of a formatting operation of a relative datetime. |
|
Represents the unit for formatting a relative date. |
|
Represents the unit for formatting a relative date. |
|
The formatting style |
Public methods | |
---|---|
String! |
combineDateAndTime(relativeDateString: String!, timeString: String!) Combines a relative date string and a time string in this object's locale. |
String! |
format(direction: RelativeDateTimeFormatter.Direction!, unit: RelativeDateTimeFormatter.AbsoluteUnit!) Formats a relative date without a quantity. |
String! |
format(quantity: Double, direction: RelativeDateTimeFormatter.Direction!, unit: RelativeDateTimeFormatter.RelativeUnit!) Formats a relative date with a quantity such as "in 5 days" or "3 months ago". |
String! |
format(offset: Double, unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!) Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e. |
String! |
formatNumeric(offset: Double, unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!) Format a combination of RelativeDateTimeUnit and numeric offset using a numeric style, e. |
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
formatNumericToValue(offset: Double, unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!) Format a combination of RelativeDateTimeUnit and numeric offset using a numeric style, e. |
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
formatToValue(direction: RelativeDateTimeFormatter.Direction!, unit: RelativeDateTimeFormatter.AbsoluteUnit!) Formats a relative date without a quantity. |
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
formatToValue(quantity: Double, direction: RelativeDateTimeFormatter.Direction!, unit: RelativeDateTimeFormatter.RelativeUnit!) Formats a relative date with a quantity such as "in 5 days" or "3 months ago". |
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
formatToValue(offset: Double, unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!) Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e. |
DisplayContext! |
Return capitalization context. |
RelativeDateTimeFormatter.Style! |
Return style |
static RelativeDateTimeFormatter! |
Returns a RelativeDateTimeFormatter for the default locale. |
static RelativeDateTimeFormatter! |
getInstance(locale: ULocale!) Returns a RelativeDateTimeFormatter for a particular locale. |
static RelativeDateTimeFormatter! |
getInstance(locale: ULocale!, nf: NumberFormat!) Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object. |
static RelativeDateTimeFormatter! |
getInstance(locale: ULocale!, nf: NumberFormat!, style: RelativeDateTimeFormatter.Style!, capitalizationContext: DisplayContext!) Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object, style, and capitalization context |
static RelativeDateTimeFormatter! |
getInstance(locale: Locale!) Returns a RelativeDateTimeFormatter for a particular |
static RelativeDateTimeFormatter! |
getInstance(locale: Locale!, nf: NumberFormat!) Returns a RelativeDateTimeFormatter for a particular |
NumberFormat! |
Returns a copy of the NumberFormat this object is using. |
Public methods
combineDateAndTime
fun combineDateAndTime(
relativeDateString: String!,
timeString: String!
): String!
Combines a relative date string and a time string in this object's locale. This is done with the same date-time separator used for the default calendar in this locale.
Parameters | |
---|---|
relativeDateString |
String!: the relative date e.g 'yesterday' |
timeString |
String!: the time e.g '3:45' |
Return | |
---|---|
String! |
the date and time concatenated according to the default calendar in this locale e.g 'yesterday, 3:45' |
format
fun format(
direction: RelativeDateTimeFormatter.Direction!,
unit: RelativeDateTimeFormatter.AbsoluteUnit!
): String!
Formats a relative date without a quantity. This method returns a String. To get more information about the formatting result, use formatToValue().
Parameters | |
---|---|
direction |
RelativeDateTimeFormatter.Direction!: NEXT, LAST, THIS, etc. |
unit |
RelativeDateTimeFormatter.AbsoluteUnit!: e.g SATURDAY, DAY, MONTH |
Return | |
---|---|
String! |
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. |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if the direction is incompatible with unit this can occur with NOW which can only take PLAIN. |
format
fun format(
quantity: Double,
direction: RelativeDateTimeFormatter.Direction!,
unit: RelativeDateTimeFormatter.RelativeUnit!
): String!
Formats a relative date with a quantity such as "in 5 days" or "3 months ago". This method returns a String. To get more information about the formatting result, use formatToValue().
Parameters | |
---|---|
quantity |
Double: The numerical amount e.g 5. This value is formatted according to this object's NumberFormat object. |
direction |
RelativeDateTimeFormatter.Direction!: NEXT means a future relative date; LAST means a past relative date. |
unit |
RelativeDateTimeFormatter.RelativeUnit!: the unit e.g day? month? year? |
Return | |
---|---|
String! |
the formatted string |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if direction is something other than NEXT or LAST. |
format
fun format(
offset: Double,
unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!
): String!
Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g. "last week", "this week", "next week", "yesterday", "tomorrow". Falls back to numeric style if no appropriate text term is available for the specified offset in the object’s locale. This method returns a String. To get more information about the formatting result, use formatToValue().
Parameters | |
---|---|
offset |
Double: The signed offset for the specified field. |
unit |
RelativeDateTimeFormatter.RelativeDateTimeUnit!: The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY. |
Return | |
---|---|
String! |
The formatted string (may be empty in case of error) |
formatNumeric
fun formatNumeric(
offset: Double,
unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!
): String!
Format 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". This method returns a String. To get more information about the formatting result, use formatNumericToValue().
Parameters | |
---|---|
offset |
Double: The signed offset for the specified unit. This will be formatted according to this object's NumberFormat object. |
unit |
RelativeDateTimeFormatter.RelativeDateTimeUnit!: The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY. |
Return | |
---|---|
String! |
The formatted string (may be empty in case of error) |
formatNumericToValue
fun formatNumericToValue(
offset: Double,
unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!
): RelativeDateTimeFormatter.FormattedRelativeDateTime!
Format 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". This method returns a FormattedRelativeDateTime, which exposes more information than the String returned by formatNumeric().
Parameters | |
---|---|
offset |
Double: The signed offset for the specified unit. This will be formatted according to this object's NumberFormat object. |
unit |
RelativeDateTimeFormatter.RelativeDateTimeUnit!: The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY. |
Return | |
---|---|
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
The formatted string (may be empty in case of error) |
formatToValue
fun formatToValue(
direction: RelativeDateTimeFormatter.Direction!,
unit: RelativeDateTimeFormatter.AbsoluteUnit!
): RelativeDateTimeFormatter.FormattedRelativeDateTime!
Formats a relative date without a quantity. This method returns a FormattedRelativeDateTime, which exposes more information than the String returned by format().
Parameters | |
---|---|
direction |
RelativeDateTimeFormatter.Direction!: NEXT, LAST, THIS, etc. |
unit |
RelativeDateTimeFormatter.AbsoluteUnit!: e.g SATURDAY, DAY, MONTH |
Return | |
---|---|
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
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. |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if the direction is incompatible with unit this can occur with NOW which can only take PLAIN. |
formatToValue
fun formatToValue(
quantity: Double,
direction: RelativeDateTimeFormatter.Direction!,
unit: RelativeDateTimeFormatter.RelativeUnit!
): RelativeDateTimeFormatter.FormattedRelativeDateTime!
Formats a relative date with a quantity such as "in 5 days" or "3 months ago". This method returns a FormattedRelativeDateTime, which exposes more information than the String returned by format().
Parameters | |
---|---|
quantity |
Double: The numerical amount e.g 5. This value is formatted according to this object's NumberFormat object. |
direction |
RelativeDateTimeFormatter.Direction!: NEXT means a future relative date; LAST means a past relative date. |
unit |
RelativeDateTimeFormatter.RelativeUnit!: the unit e.g day? month? year? |
Return | |
---|---|
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
the formatted relative datetime |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if direction is something other than NEXT or LAST. |
formatToValue
fun formatToValue(
offset: Double,
unit: RelativeDateTimeFormatter.RelativeDateTimeUnit!
): RelativeDateTimeFormatter.FormattedRelativeDateTime!
Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g. "last week", "this week", "next week", "yesterday", "tomorrow". Falls back to numeric style if no appropriate text term is available for the specified offset in the object’s locale. This method returns a FormattedRelativeDateTime, which exposes more information than the String returned by format().
Parameters | |
---|---|
offset |
Double: The signed offset for the specified field. |
unit |
RelativeDateTimeFormatter.RelativeDateTimeUnit!: The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY. |
Return | |
---|---|
RelativeDateTimeFormatter.FormattedRelativeDateTime! |
The formatted string (may be empty in case of error) |
getCapitalizationContext
fun getCapitalizationContext(): DisplayContext!
Return capitalization context.
Return | |
---|---|
DisplayContext! |
The capitalization context. |
getFormatStyle
fun getFormatStyle(): RelativeDateTimeFormatter.Style!
Return style
Return | |
---|---|
RelativeDateTimeFormatter.Style! |
The formatting style. |
getInstance
static fun getInstance(): RelativeDateTimeFormatter!
Returns a RelativeDateTimeFormatter for the default locale.
getInstance
static fun getInstance(locale: ULocale!): RelativeDateTimeFormatter!
Returns a RelativeDateTimeFormatter for a particular locale.
Parameters | |
---|---|
locale |
ULocale!: the locale. |
Return | |
---|---|
RelativeDateTimeFormatter! |
An instance of RelativeDateTimeFormatter. |
getInstance
static fun getInstance(
locale: ULocale!,
nf: NumberFormat!
): RelativeDateTimeFormatter!
Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object.
Parameters | |
---|---|
locale |
ULocale!: the locale |
nf |
NumberFormat!: the number format object. It is defensively copied to ensure thread-safety and immutability of this class. |
Return | |
---|---|
RelativeDateTimeFormatter! |
An instance of RelativeDateTimeFormatter. |
getInstance
static fun getInstance(
locale: ULocale!,
nf: NumberFormat!,
style: RelativeDateTimeFormatter.Style!,
capitalizationContext: DisplayContext!
): RelativeDateTimeFormatter!
Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object, style, and capitalization context
Parameters | |
---|---|
locale |
ULocale!: the locale |
nf |
NumberFormat!: the number format object. It is defensively copied to ensure thread-safety and immutability of this class. May be null. |
style |
RelativeDateTimeFormatter.Style!: the style. |
capitalizationContext |
DisplayContext!: the capitalization context. |
getInstance
static fun getInstance(locale: Locale!): RelativeDateTimeFormatter!
Returns a RelativeDateTimeFormatter for a particular java.util.Locale
.
Parameters | |
---|---|
locale |
Locale!: the java.util.Locale . |
Return | |
---|---|
RelativeDateTimeFormatter! |
An instance of RelativeDateTimeFormatter. |
getInstance
static fun getInstance(
locale: Locale!,
nf: NumberFormat!
): RelativeDateTimeFormatter!
Returns a RelativeDateTimeFormatter for a particular java.util.Locale
that uses a particular NumberFormat object.
Parameters | |
---|---|
locale |
Locale!: the java.util.Locale |
nf |
NumberFormat!: the number format object. It is defensively copied to ensure thread-safety and immutability of this class. |
Return | |
---|---|
RelativeDateTimeFormatter! |
An instance of RelativeDateTimeFormatter. |
getNumberFormat
fun getNumberFormat(): NumberFormat!
Returns a copy of the NumberFormat this object is using.
Return | |
---|---|
NumberFormat! |
A copy of the NumberFormat. |