public final class PrettyTime extends Object
Enables formatted output as usually used in social media in different languages.
Parsing is not included because there is no general solution for all locales. Instead users must keep the backing duration object and use it for printing.
| Modifier and Type | Method and Description |
|---|---|
Locale |
getLocale()
Gets the language of this instance.
|
TimeSource<?> |
getReferenceClock()
Yields the reference clock for formatting of relative times.
|
static PrettyTime |
of(Locale locale)
Gets an instance of
PrettyTime for given language,
possibly cached. |
String |
print(Duration<?> duration,
TextWidth width)
Formats given duration.
|
String |
print(long amount,
CalendarUnit unit,
TextWidth width)
Formats given duration in calendar units.
|
String |
print(long amount,
ClockUnit unit,
TextWidth width)
Formats given duration in clock units.
|
String |
printRelative(UnixTime moment,
String tzid)
Formats given time point relative to the current time of
getReferenceClock() as duration in at most second
precision or less. |
String |
printRelative(UnixTime moment,
TZID tzid)
Formats given time point relative to the current time of
getReferenceClock() as duration in at most second
precision or less. |
PrettyTime |
withEmptyUnit(CalendarUnit emptyUnit)
Defines the time unit used for formatting an empty duration.
|
PrettyTime |
withEmptyUnit(ClockUnit emptyUnit)
Defines the time unit used for formatting an empty duration.
|
PrettyTime |
withReferenceClock(TimeSource<?> clock)
Yields a changed copy of this instance with given reference
clock.
|
PrettyTime |
withZeroDigit(char zeroDigit)
Defines the localized zero digit.
|
public static PrettyTime of(Locale locale)
Gets an instance of PrettyTime for given language,
possibly cached.
locale - the language an instance is searched forpublic Locale getLocale()
Gets the language of this instance.
public TimeSource<?> getReferenceClock()
Yields the reference clock for formatting of relative times.
withReferenceClock(TimeSource),
printRelative(UnixTime, TZID),
printRelative(UnixTime, String)public PrettyTime withReferenceClock(TimeSource<?> clock)
Yields a changed copy of this instance with given reference clock.
If given reference clock is null then the reference clock
will always be the system clock.
clock - new reference clock (maybe null)PrettyTime with changed reference clockgetReferenceClock(),
printRelative(UnixTime, TZID),
printRelative(UnixTime, String)public PrettyTime withZeroDigit(char zeroDigit)
Defines the localized zero digit.
In most languages the zero digit is just ASCII-"0", but
in arabic locales the digit can also be the char U+0660. By
default Time4J will try to use the JDK-setting. This method can override
it however.
zeroDigit - localized zero digitDecimalFormatSymbols.getZeroDigit()public PrettyTime withEmptyUnit(CalendarUnit emptyUnit)
Defines the time unit used for formatting an empty duration.
Time4J uses seconds as default. This method can override the default however.
emptyUnit - time unit for usage in an empty durationpublic PrettyTime withEmptyUnit(ClockUnit emptyUnit)
Defines the time unit used for formatting an empty duration.
Time4J uses seconds as default. This method can override the default however.
emptyUnit - time unit for usage in an empty durationpublic String print(long amount, CalendarUnit unit, TextWidth width)
Formats given duration in calendar units.
Note: Millennia, centuries and decades are automatically normalized to years while quarter-years are normalized to months.
amount - count of units (quantity)unit - calendar unitwidth - text width (ABBREVIATED as synonym for SHORT)print(Duration, TextWidth)public String print(long amount, ClockUnit unit, TextWidth width)
Formats given duration in clock units.
amount - count of units (quantity)unit - clock unitwidth - text width (ABBREVIATED as synonym for SHORT)print(Duration, TextWidth)public String print(Duration<?> duration, TextWidth width)
Formats given duration.
A localized output is only supported for the units
CalendarUnit.YEARS, CalendarUnit.MONTHS,
CalendarUnit.WEEKS, CalendarUnit.DAYS and
all ClockUnit-units. This methode does not perform
any normalization.
Note: If the local script variant is from right to left then a unicode-RLM-marker will automatically be inserted before each number.
duration - object representing a duration which might contain
several units and quantitieswidth - text width (ABBREVIATED as synonym for SHORT)public String printRelative(UnixTime moment, TZID tzid)
Formats given time point relative to the current time of
getReferenceClock() as duration in at most second
precision or less.
moment - relative time pointtzid - time zone id for translating to a local durationpublic String printRelative(UnixTime moment, String tzid)
Formats given time point relative to the current time of
getReferenceClock() as duration in at most second
precision or less.
moment - relative time pointtzid - time zone id for translating to a local durationCopyright © 2014. All rights reserved.