Class RelativeDateTimeFormatter
- java.lang.Object
-
- com.ibm.icu.text.RelativeDateTimeFormatter
-
public final class RelativeDateTimeFormatter extends java.lang.Object
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RelativeDateTimeFormatter.AbsoluteUnit
Represents an absolute unit.private static class
RelativeDateTimeFormatter.Cache
static class
RelativeDateTimeFormatter.Direction
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday"static class
RelativeDateTimeFormatter.Field
Field constants used when accessing field information for relative datetime strings in FormattedValue.static class
RelativeDateTimeFormatter.FormattedRelativeDateTime
Represents the result of a formatting operation of a relative datetime.private static class
RelativeDateTimeFormatter.Loader
private static class
RelativeDateTimeFormatter.RelativeDateTimeFormatterData
static class
RelativeDateTimeFormatter.RelativeDateTimeUnit
Represents the unit for formatting a relative date.static class
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.static class
RelativeDateTimeFormatter.Style
The formatting style
-
Field Summary
Fields Modifier and Type Field Description private BreakIterator
breakIterator
private static RelativeDateTimeFormatter.Cache
cache
private DisplayContext
capitalizationContext
private java.lang.String
combinedDateAndTime
private DateFormatSymbols
dateFormatSymbols
private static RelativeDateTimeFormatter.Style[]
fallbackCache
private ULocale
locale
private NumberFormat
numberFormat
private java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.RelativeUnit,java.lang.String[][]>>
patternMap
private PluralRules
pluralRules
private java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.AbsoluteUnit,java.util.EnumMap<RelativeDateTimeFormatter.Direction,java.lang.String>>>
qualitativeUnitMap
private RelativeDateTimeFormatter.Style
style
private int[]
styleToDateFormatSymbolsWidth
-
Constructor Summary
Constructors Modifier Constructor Description private
RelativeDateTimeFormatter(java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.AbsoluteUnit,java.util.EnumMap<RelativeDateTimeFormatter.Direction,java.lang.String>>> qualitativeUnitMap, java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.RelativeUnit,java.lang.String[][]>> patternMap, java.lang.String combinedDateAndTime, PluralRules pluralRules, NumberFormat numberFormat, RelativeDateTimeFormatter.Style style, DisplayContext capitalizationContext, BreakIterator breakIterator, ULocale locale)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
adjustForContext(java.lang.String originalFormattedString)
private void
checkNoAdjustForContext()
java.lang.String
combineDateAndTime(java.lang.String relativeDateString, java.lang.String timeString)
Combines a relative date string and a time string in this object's locale.java.lang.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".java.lang.String
format(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Format a combination of RelativeDateTimeUnit and numeric offset using a text style if possible, e.g.java.lang.String
format(RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.AbsoluteUnit unit)
Formats a relative date without a quantity.private java.lang.String
formatAbsoluteImpl(RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.AbsoluteUnit unit)
Implementation method for format and formatToValue with AbsoluteUnitprivate FormattedStringBuilder
formatImpl(double quantity, RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.RelativeUnit unit)
Implementation method for format and formatToValue with RelativeUnitjava.lang.String
formatNumeric(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Format a combination of RelativeDateTimeUnit and numeric offset using a numeric style, e.g.private FormattedStringBuilder
formatNumericImpl(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Implementation method for formatNumeric and formatNumericToValueRelativeDateTimeFormatter.FormattedRelativeDateTime
formatNumericToValue(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Format a combination of RelativeDateTimeUnit and numeric offset using a numeric style, e.g.private java.lang.CharSequence
formatRelativeImpl(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Implementation method for format and formatToValue with RelativeDateTimeUnit.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.RelativeDateTimeFormatter.FormattedRelativeDateTime
formatToValue(RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.AbsoluteUnit unit)
Formats a relative date without a quantity.private java.lang.String
getAbsoluteUnitString(RelativeDateTimeFormatter.Style style, RelativeDateTimeFormatter.AbsoluteUnit unit, RelativeDateTimeFormatter.Direction direction)
Gets the string value from qualitativeUnitMap with fallback based on style.DisplayContext
getCapitalizationContext()
Return capitalization context.RelativeDateTimeFormatter.Style
getFormatStyle()
Return stylestatic RelativeDateTimeFormatter
getInstance()
Returns a RelativeDateTimeFormatter for the default locale.static RelativeDateTimeFormatter
getInstance(ULocale 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(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 contextstatic RelativeDateTimeFormatter
getInstance(java.util.Locale locale)
Returns a RelativeDateTimeFormatter for a particularLocale
.static RelativeDateTimeFormatter
getInstance(java.util.Locale locale, NumberFormat nf)
Returns a RelativeDateTimeFormatter for a particularLocale
that uses a particular NumberFormat object.NumberFormat
getNumberFormat()
Returns a copy of the NumberFormat this object is using.private java.lang.String
getRelativeUnitPattern(RelativeDateTimeFormatter.Style style, RelativeDateTimeFormatter.RelativeUnit unit, int pastFutureIndex, StandardPlural pluralForm)
private java.lang.String
getRelativeUnitPluralPattern(RelativeDateTimeFormatter.Style style, RelativeDateTimeFormatter.RelativeUnit unit, int pastFutureIndex, StandardPlural pluralForm)
private static RelativeDateTimeFormatter.Direction
keyToDirection(UResource.Key key)
-
-
-
Field Detail
-
styleToDateFormatSymbolsWidth
private int[] styleToDateFormatSymbolsWidth
-
qualitativeUnitMap
private final java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.AbsoluteUnit,java.util.EnumMap<RelativeDateTimeFormatter.Direction,java.lang.String>>> qualitativeUnitMap
-
patternMap
private final java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.RelativeUnit,java.lang.String[][]>> patternMap
-
combinedDateAndTime
private final java.lang.String combinedDateAndTime
-
pluralRules
private final PluralRules pluralRules
-
numberFormat
private final NumberFormat numberFormat
-
style
private final RelativeDateTimeFormatter.Style style
-
capitalizationContext
private final DisplayContext capitalizationContext
-
breakIterator
private final BreakIterator breakIterator
-
locale
private final ULocale locale
-
dateFormatSymbols
private final DateFormatSymbols dateFormatSymbols
-
fallbackCache
private static final RelativeDateTimeFormatter.Style[] fallbackCache
-
cache
private static final RelativeDateTimeFormatter.Cache cache
-
-
Constructor Detail
-
RelativeDateTimeFormatter
private RelativeDateTimeFormatter(java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.AbsoluteUnit,java.util.EnumMap<RelativeDateTimeFormatter.Direction,java.lang.String>>> qualitativeUnitMap, java.util.EnumMap<RelativeDateTimeFormatter.Style,java.util.EnumMap<RelativeDateTimeFormatter.RelativeUnit,java.lang.String[][]>> patternMap, java.lang.String combinedDateAndTime, PluralRules pluralRules, NumberFormat numberFormat, RelativeDateTimeFormatter.Style style, DisplayContext capitalizationContext, BreakIterator breakIterator, ULocale locale)
-
-
Method Detail
-
getInstance
public static RelativeDateTimeFormatter getInstance()
Returns a RelativeDateTimeFormatter for the default locale.
-
getInstance
public static RelativeDateTimeFormatter getInstance(ULocale locale)
Returns a RelativeDateTimeFormatter for a particular locale.- Parameters:
locale
- the locale.- Returns:
- An instance of RelativeDateTimeFormatter.
-
getInstance
public static RelativeDateTimeFormatter getInstance(java.util.Locale locale)
Returns a RelativeDateTimeFormatter for a particularLocale
.- Parameters:
locale
- theLocale
.- Returns:
- An instance of RelativeDateTimeFormatter.
-
getInstance
public static RelativeDateTimeFormatter getInstance(ULocale locale, NumberFormat nf)
Returns a RelativeDateTimeFormatter for a particular locale that uses a particular NumberFormat object.- Parameters:
locale
- the localenf
- the number format object. It is defensively copied to ensure thread-safety and immutability of this class.- Returns:
- An instance of RelativeDateTimeFormatter.
-
getInstance
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
- the localenf
- the number format object. It is defensively copied to ensure thread-safety and immutability of this class. May be null.style
- the style.capitalizationContext
- the capitalization context.
-
getInstance
public static RelativeDateTimeFormatter getInstance(java.util.Locale locale, NumberFormat nf)
Returns a RelativeDateTimeFormatter for a particularLocale
that uses a particular NumberFormat object.- Parameters:
locale
- theLocale
nf
- the number format object. It is defensively copied to ensure thread-safety and immutability of this class.- Returns:
- An instance of RelativeDateTimeFormatter.
-
format
public java.lang.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
- The numerical amount e.g 5. This value is formatted according to this object'sNumberFormat
object.direction
- NEXT means a future relative date; LAST means a past relative date.unit
- the unit e.g day? month? year?- Returns:
- the formatted string
- Throws:
java.lang.IllegalArgumentException
- if direction is something other than NEXT or LAST.
-
formatToValue
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
- The numerical amount e.g 5. This value is formatted according to this object'sNumberFormat
object.direction
- NEXT means a future relative date; LAST means a past relative date.unit
- the unit e.g day? month? year?- Returns:
- the formatted relative datetime
- Throws:
java.lang.IllegalArgumentException
- if direction is something other than NEXT or LAST.
-
formatImpl
private FormattedStringBuilder formatImpl(double quantity, RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.RelativeUnit unit)
Implementation method for format and formatToValue with RelativeUnit
-
formatNumeric
public java.lang.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
- The signed offset for the specified unit. This will be formatted according to this object's NumberFormat object.unit
- The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY.- Returns:
- The formatted string (may be empty in case of error)
-
formatNumericToValue
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
- The signed offset for the specified unit. This will be formatted according to this object's NumberFormat object.unit
- The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY.- Returns:
- The formatted string (may be empty in case of error)
-
formatNumericImpl
private FormattedStringBuilder formatNumericImpl(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Implementation method for formatNumeric and formatNumericToValue
-
format
public java.lang.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
- NEXT, LAST, THIS, etc.unit
- e.g SATURDAY, DAY, MONTH- 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.
- Throws:
java.lang.IllegalArgumentException
- if the direction is incompatible with unit this can occur with NOW which can only take PLAIN.
-
formatToValue
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
- NEXT, LAST, THIS, etc.unit
- e.g SATURDAY, DAY, MONTH- 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.
- Throws:
java.lang.IllegalArgumentException
- if the direction is incompatible with unit this can occur with NOW which can only take PLAIN.
-
formatAbsoluteImpl
private java.lang.String formatAbsoluteImpl(RelativeDateTimeFormatter.Direction direction, RelativeDateTimeFormatter.AbsoluteUnit unit)
Implementation method for format and formatToValue with AbsoluteUnit
-
format
public java.lang.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’s locale. This method returns a String. To get more information about the formatting result, use formatToValue().- Parameters:
offset
- The signed offset for the specified field.unit
- The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY.- Returns:
- The formatted string (may be empty in case of error)
-
formatToValue
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’s locale. This method returns a FormattedRelativeDateTime, which exposes more information than the String returned by format().- Parameters:
offset
- The signed offset for the specified field.unit
- The unit to use when formatting the relative date, e.g. RelativeDateTimeUnit.WEEK, RelativeDateTimeUnit.FRIDAY.- Returns:
- The formatted string (may be empty in case of error)
-
formatRelativeImpl
private java.lang.CharSequence formatRelativeImpl(double offset, RelativeDateTimeFormatter.RelativeDateTimeUnit unit)
Implementation method for format and formatToValue with RelativeDateTimeUnit.
-
getAbsoluteUnitString
private java.lang.String getAbsoluteUnitString(RelativeDateTimeFormatter.Style style, RelativeDateTimeFormatter.AbsoluteUnit unit, RelativeDateTimeFormatter.Direction direction)
Gets the string value from qualitativeUnitMap with fallback based on style.
-
combineDateAndTime
public java.lang.String combineDateAndTime(java.lang.String relativeDateString, java.lang.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
- the relative date e.g 'yesterday'timeString
- the time e.g '3:45'- Returns:
- the date and time concatenated according to the default calendar in this locale e.g 'yesterday, 3:45'
-
getNumberFormat
public NumberFormat getNumberFormat()
Returns a copy of the NumberFormat this object is using.- Returns:
- A copy of the NumberFormat.
-
getCapitalizationContext
public DisplayContext getCapitalizationContext()
Return capitalization context.- Returns:
- The capitalization context.
-
getFormatStyle
public RelativeDateTimeFormatter.Style getFormatStyle()
Return style- Returns:
- The formatting style.
-
adjustForContext
private java.lang.String adjustForContext(java.lang.String originalFormattedString)
-
checkNoAdjustForContext
private void checkNoAdjustForContext()
-
getRelativeUnitPluralPattern
private java.lang.String getRelativeUnitPluralPattern(RelativeDateTimeFormatter.Style style, RelativeDateTimeFormatter.RelativeUnit unit, int pastFutureIndex, StandardPlural pluralForm)
-
getRelativeUnitPattern
private java.lang.String getRelativeUnitPattern(RelativeDateTimeFormatter.Style style, RelativeDateTimeFormatter.RelativeUnit unit, int pastFutureIndex, StandardPlural pluralForm)
-
keyToDirection
private static RelativeDateTimeFormatter.Direction keyToDirection(UResource.Key key)
-
-