RelativeDateTimeFormatter

public final class RelativeDateTimeFormatter
extends Object

java.lang.Object
   ↳ 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

class RelativeDateTimeFormatter.FormattedRelativeDateTime

Represents the result of a formatting operation of a relative datetime. 

Public methods

String combineDateAndTime(String relativeDateString, String timeString)

Combines a relative date string and a time string in this object's locale.

String format(double quantity, RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.RelativeUnit unit)

Formats a relative date with a quantity such as "in 5 days" or "3 months ago".

String format(RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.AbsoluteUnit unit)

Formats a relative date without a quantity.

String format(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g. "last week", "this week", "next week", "yesterday", "tomorrow".

String formatNumeric(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

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".

RelativeDateTimeFormatter.FormattedRelativeDateTime formatNumericToValue(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

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".

RelativeDateTimeFormatter.FormattedRelativeDateTime formatToValue(double quantity, RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.RelativeUnit unit)

Formats a relative date with a quantity such as "in 5 days" or "3 months ago".

RelativeDateTimeFormatter.FormattedRelativeDateTime formatToValue(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g. "last week", "this week", "next week", "yesterday", "tomorrow".

RelativeDateTimeFormatter.FormattedRelativeDateTime formatToValue(RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.AbsoluteUnit unit)

Formats a relative date without a quantity.

DisplayContext getCapitalizationContext()

Return capitalization context.

RelativeDateTimeFormatter.Style getFormatStyle()

Return style

static RelativeDateTimeFormatter getInstance(ULocale locale, NumberFormat nf, RelativeDateTimeFormatter.Style style, DisplayContext capitalizationContext)

Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object, style, and capitalization context

static RelativeDateTimeFormatter getInstance(ULocale locale)

Returns a RelativeDateTimeFormatter for a particular locale.

static RelativeDateTimeFormatter getInstance()

Returns a RelativeDateTimeFormatter for the default locale.

static RelativeDateTimeFormatter getInstance(Locale locale)

Returns a RelativeDateTimeFormatter for a particular Locale.

static RelativeDateTimeFormatter getInstance(ULocale locale, NumberFormat nf)

Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object.

static RelativeDateTimeFormatter getInstance(Locale locale, NumberFormat nf)

Returns a RelativeDateTimeFormatter for a particular Locale that uses a particular NumberFormat object.

NumberFormat getNumberFormat()

Returns a copy of the NumberFormat this object is using.

Inherited methods

Public methods

combineDateAndTime

Added in API level 24
public String combineDateAndTime (String relativeDateString, 
                String timeString)

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'

Returns
String the date and time concatenated according to the default calendar in this locale e.g 'yesterday, 3:45'

format

Added in API level 24
public String format (double quantity, 
                RelativeDateTimeFormatter.Direction direction, 
                RelativeDateTimeFormatter.RelativeUnit unit)

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?

Returns
String the formatted string

Throws
IllegalArgumentException if direction is something other than NEXT or LAST.

format

Added in API level 24
public String format (RelativeDateTimeFormatter.Direction direction, 
                RelativeDateTimeFormatter.AbsoluteUnit unit)

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

Returns
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.

Throws
IllegalArgumentException if the direction is incompatible with unit this can occur with NOW which can only take PLAIN.

format

Added in API level 28
public String format (double offset, 
                RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

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\u2019s 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.

Returns
String The formatted string (may be empty in case of error)

formatNumeric

Added in API level 28
public String formatNumeric (double offset, 
                RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

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.

Returns
String The formatted string (may be empty in case of error)

formatNumericToValue

Added in API level 31
public RelativeDateTimeFormatter.FormattedRelativeDateTime formatNumericToValue (double offset, 
                RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

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.

Returns
RelativeDateTimeFormatter.FormattedRelativeDateTime The formatted string (may be empty in case of error)

formatToValue

Added in API level 31
public RelativeDateTimeFormatter.FormattedRelativeDateTime formatToValue (double quantity, 
                RelativeDateTimeFormatter.Direction direction, 
                RelativeDateTimeFormatter.RelativeUnit unit)

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?

Returns
RelativeDateTimeFormatter.FormattedRelativeDateTime the formatted relative datetime

Throws
IllegalArgumentException if direction is something other than NEXT or LAST.

formatToValue

Added in API level 31
public RelativeDateTimeFormatter.FormattedRelativeDateTime formatToValue (double offset, 
                RelativeDateTimeFormatter.RelativeDateTimeUnit unit)

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\u2019s 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.

Returns
RelativeDateTimeFormatter.FormattedRelativeDateTime The formatted string (may be empty in case of error)

formatToValue

Added in API level 31
public RelativeDateTimeFormatter.FormattedRelativeDateTime formatToValue (RelativeDateTimeFormatter.Direction direction, 
                RelativeDateTimeFormatter.AbsoluteUnit unit)

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

Returns
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.

Throws
IllegalArgumentException if the direction is incompatible with unit this can occur with NOW which can only take PLAIN.

getCapitalizationContext

Added in API level 24
public DisplayContext getCapitalizationContext ()

Return capitalization context.

Returns
DisplayContext The capitalization context.

getFormatStyle

Added in API level 24
public RelativeDateTimeFormatter.Style getFormatStyle ()

Return style

Returns
RelativeDateTimeFormatter.Style The formatting style.

getInstance

Added in API level 24
public static RelativeDateTimeFormatter getInstance (ULocale locale, 
                NumberFormat nf, 
                RelativeDateTimeFormatter.Style style, 
                DisplayContext capitalizationContext)

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.

Returns
RelativeDateTimeFormatter

getInstance

Added in API level 24
public static RelativeDateTimeFormatter getInstance (ULocale locale)

Returns a RelativeDateTimeFormatter for a particular locale.

Parameters
locale ULocale: the locale.

Returns
RelativeDateTimeFormatter An instance of RelativeDateTimeFormatter.

getInstance

Added in API level 24
public static RelativeDateTimeFormatter getInstance ()

Returns a RelativeDateTimeFormatter for the default locale.

Returns
RelativeDateTimeFormatter

getInstance

Added in API level 24
public static RelativeDateTimeFormatter getInstance (Locale locale)

Returns a RelativeDateTimeFormatter for a particular Locale.

Parameters
locale Locale: the Locale.

Returns
RelativeDateTimeFormatter An instance of RelativeDateTimeFormatter.

getInstance

Added in API level 24
public static RelativeDateTimeFormatter getInstance (ULocale locale, 
                NumberFormat nf)

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.

Returns
RelativeDateTimeFormatter An instance of RelativeDateTimeFormatter.

getInstance

Added in API level 24
public static RelativeDateTimeFormatter getInstance (Locale locale, 
                NumberFormat nf)

Returns a RelativeDateTimeFormatter for a particular Locale that uses a particular NumberFormat object.

Parameters
locale Locale: the Locale

nf NumberFormat: the number format object. It is defensively copied to ensure thread-safety and immutability of this class.

Returns
RelativeDateTimeFormatter An instance of RelativeDateTimeFormatter.

getNumberFormat

Added in API level 24
public NumberFormat getNumberFormat ()

Returns a copy of the NumberFormat this object is using.

Returns
NumberFormat A copy of the NumberFormat.