Most visited

Recently visited

Added in API level 24

RelativeDateTimeFormatter

public final class RelativeDateTimeFormatter
extends Object

java.lang.Object
   ↳ android.icu.text.RelativeDateTimeFormatter


格式化简单的相对日期。 它处理的相对日期有两种类型:

这个API是非常基础的,并且旨在成为更多花哨的API的构建块。 调用者确切地告诉它要以独立于区域的方式显示的内容。 虽然这个类自动提供了正确的复数形式,但其语法形式应尽可能保持中立。 来电者有责任处理截止逻辑,例如决定在“7天内”还是“1周内”之间进行显示。 该API支持涉及单个单元的相对日期。 此API不支持涉及复合单位的相对日期。 例如“5天4小时”,也不支持解析。 这个类既是不可变的,也是线程安全的。

以下是一些使用示例:

 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"
 

未来,我们可能会添加更多表单,如缩短/短格式(3秒前)和相对日期(“昨天下午”)等。

Summary

Nested classes

枚举 RelativeDateTimeFormatter.AbsoluteUnit

代表一个绝对单位。

枚举 RelativeDateTimeFormatter.Direction

代表绝对单位的方向,例如“下周二”或“上周二”

枚举 RelativeDateTimeFormatter.RelativeUnit

表示格式化相对日期的单位。

枚举 RelativeDateTimeFormatter.Style

格式化风格

Public methods

String combineDateAndTime(String relativeDateString, String timeString)

在此对象的语言环境中组合相对日期字符串和时间字符串。

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

使用诸如“在5天内”或“3个月前”之类的数量格式化相对日期

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

格式化没有数量的相对日期。

DisplayContext getCapitalizationContext()

返回大写上下文。

RelativeDateTimeFormatter.Style getFormatStyle()

返回样式

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

为使用特定NumberFormat对象,样式和大小写上下文的特定语言环境返回RelativeDateTimeFormatter

static RelativeDateTimeFormatter getInstance(ULocale locale)

为特定语言环境返回RelativeDateTimeFormatter。

static RelativeDateTimeFormatter getInstance()

返回默认语言环境的RelativeDateTimeFormatter。

static RelativeDateTimeFormatter getInstance(Locale locale)

为特定的 Locale返回RelativeDateTimeFormatter。

static RelativeDateTimeFormatter getInstance(ULocale locale, NumberFormat nf)

为使用特定NumberFormat对象的特定语言环境返回RelativeDateTimeFormatter。

static RelativeDateTimeFormatter getInstance(Locale locale, NumberFormat nf)

返回使用特定NumberFormat对象的特定 Locale的RelativeDateTimeFormatter。

NumberFormat getNumberFormat()

返回此对象正在使用的NumberFormat的副本。

Inherited methods

From class java.lang.Object

Public methods

combineDateAndTime

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

在此对象的语言环境中组合相对日期字符串和时间字符串。 这是通过在此语言环境中使用用于默认日历的相同日期时间分隔符完成的。

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
String format (double quantity, 
                RelativeDateTimeFormatter.Direction direction, 
                RelativeDateTimeFormatter.RelativeUnit unit)

使用诸如“在5天内”或“3个月前”之类的数量格式化相对日期

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
String format (RelativeDateTimeFormatter.Direction direction, 
                RelativeDateTimeFormatter.AbsoluteUnit unit)

格式化没有数量的相对日期。

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.

getCapitalizationContext

Added in API level 24
DisplayContext getCapitalizationContext ()

返回大写上下文。

Returns
DisplayContext

getFormatStyle

Added in API level 24
RelativeDateTimeFormatter.Style getFormatStyle ()

返回样式

Returns
RelativeDateTimeFormatter.Style

getInstance

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

为使用特定NumberFormat对象,样式和大小写上下文的特定语言环境返回RelativeDateTimeFormatter

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
RelativeDateTimeFormatter getInstance (ULocale locale)

为特定语言环境返回RelativeDateTimeFormatter。

Parameters
locale ULocale: the locale.
Returns
RelativeDateTimeFormatter An instance of RelativeDateTimeFormatter.

getInstance

Added in API level 24
RelativeDateTimeFormatter getInstance ()

返回默认语言环境的RelativeDateTimeFormatter。

Returns
RelativeDateTimeFormatter

getInstance

Added in API level 24
RelativeDateTimeFormatter getInstance (Locale locale)

为特定的 Locale返回RelativeDateTimeFormatter。

Parameters
locale Locale: the Locale.
Returns
RelativeDateTimeFormatter An instance of RelativeDateTimeFormatter.

getInstance

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

为使用特定NumberFormat对象的特定语言环境返回RelativeDateTimeFormatter。

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
RelativeDateTimeFormatter getInstance (Locale locale, 
                NumberFormat nf)

返回使用特定NumberFormat对象的特定 Locale的RelativeDateTimeFormatter。

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
NumberFormat getNumberFormat ()

返回此对象正在使用的NumberFormat的副本。

Returns
NumberFormat A copy of the NumberFormat.

Hooray!