Package libcore.icu
Class RelativeDateTimeFormatter
- java.lang.Object
-
- libcore.icu.RelativeDateTimeFormatter
-
public final class RelativeDateTimeFormatter extends Object
Exposes icu4j's RelativeDateTimeFormatter.
-
-
Field Summary
Fields Modifier and Type Field Description static longDAY_IN_MILLISstatic longHOUR_IN_MILLISstatic longMINUTE_IN_MILLISstatic longSECOND_IN_MILLISstatic longWEEK_IN_MILLISstatic longYEAR_IN_MILLIS
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetRelativeDateTimeString(Locale locale, TimeZone tz, long time, long now, long minResolution, long transitionResolution, int flags)This is the internal API that implements DateUtils.getRelativeDateTimeString(long, long, long, long, int), which is to return a string describing 'time' as a time relative to 'now', formatted like '[relative time/date], [time]'.static StringgetRelativeTimeSpanString(Locale locale, TimeZone tz, long time, long now, long minResolution, int flags)This is the internal API that implements the functionality of DateUtils.getRelativeTimeSpanString(long, long, long, int), which is to return a string describing 'time' as a time relative to 'now' such as '5 minutes ago', or 'In 2 days'.static StringgetRelativeTimeSpanString(Locale locale, TimeZone tz, long time, long now, long minResolution, int flags, DisplayContext displayContext)
-
-
-
Field Detail
-
SECOND_IN_MILLIS
public static final long SECOND_IN_MILLIS
- See Also:
- Constant Field Values
-
MINUTE_IN_MILLIS
public static final long MINUTE_IN_MILLIS
- See Also:
- Constant Field Values
-
HOUR_IN_MILLIS
public static final long HOUR_IN_MILLIS
- See Also:
- Constant Field Values
-
DAY_IN_MILLIS
public static final long DAY_IN_MILLIS
- See Also:
- Constant Field Values
-
WEEK_IN_MILLIS
public static final long WEEK_IN_MILLIS
- See Also:
- Constant Field Values
-
YEAR_IN_MILLIS
public static final long YEAR_IN_MILLIS
- See Also:
- Constant Field Values
-
-
Method Detail
-
getRelativeTimeSpanString
public static String getRelativeTimeSpanString(Locale locale, TimeZone tz, long time, long now, long minResolution, int flags)
This is the internal API that implements the functionality of DateUtils.getRelativeTimeSpanString(long, long, long, int), which is to return a string describing 'time' as a time relative to 'now' such as '5 minutes ago', or 'In 2 days'. More examples can be found in DateUtils' doc. In the implementation below, it selects the appropriate time unit based on the elapsed time between time' and 'now', e.g. minutes, days and etc. Callers may also specify the desired minimum resolution to show in the result. For example, '45 minutes ago' will become '0 hours ago' when minResolution is HOUR_IN_MILLIS. Once getting the quantity and unit to display, it calls icu4j's RelativeDateTimeFormatter to format the actual string according to the given locale. Note that when minResolution is set to DAY_IN_MILLIS, it returns the result depending on the actual date difference. For example, it will return 'Yesterday' even if 'time' was less than 24 hours ago but falling onto a different calendar day. It takes two additional parameters of Locale and TimeZone than the DateUtils' API. Caller must specify the locale and timezone. FORMAT_ABBREV_RELATIVE or FORMAT_ABBREV_ALL can be set in 'flags' to get the abbreviated forms when available. When 'time' equals to 'now', it always // returns a string like '0 seconds/minutes/... ago' according to minResolution.
-
getRelativeTimeSpanString
public static String getRelativeTimeSpanString(Locale locale, TimeZone tz, long time, long now, long minResolution, int flags, DisplayContext displayContext)
-
getRelativeDateTimeString
public static String getRelativeDateTimeString(Locale locale, TimeZone tz, long time, long now, long minResolution, long transitionResolution, int flags)
This is the internal API that implements DateUtils.getRelativeDateTimeString(long, long, long, long, int), which is to return a string describing 'time' as a time relative to 'now', formatted like '[relative time/date], [time]'. More examples can be found in DateUtils' doc. The function is similar to getRelativeTimeSpanString, but it always appends the absolute time to the relative time string to return '[relative time/date clause], [absolute time clause]'. It also takes an extra parameter transitionResolution to determine the format of the date clause. When the elapsed time is less than the transition resolution, it displays the relative time string. Otherwise, it gives the absolute numeric date string as the date clause. With the date and time clauses, it relies on icu4j's RelativeDateTimeFormatter::combineDateAndTime() to concatenate the two. It takes two additional parameters of Locale and TimeZone than the DateUtils' API. Caller must specify the locale and timezone. FORMAT_ABBREV_RELATIVE or FORMAT_ABBREV_ALL can be set in 'flags' to get the abbreviated forms when they are available. Bug 5252772: Since the absolute time will always be part of the result, minResolution will be set to at least DAY_IN_MILLIS to correctly indicate the date difference. For example, when it's 1:30 AM, it will return 'Yesterday, 11:30 PM' for getRelativeDateTimeString(null, null, now - 2 hours, now, HOUR_IN_MILLIS, DAY_IN_MILLIS, 0), instead of '2 hours ago, 11:30 PM' even with minResolution being HOUR_IN_MILLIS.
-
-