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 the total given duration.
|
String |
print(Duration<?> duration,
TextWidth width,
boolean printZero,
int maxLength)
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 |
withWeeksToDays()
Determines that weeks will always be normalized to days.
|
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.
clock - new reference clockPrettyTime 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 durationprint(Duration, TextWidth)public 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 durationprint(Duration, TextWidth)public PrettyTime withWeeksToDays()
Determines that weeks will always be normalized to days.
public 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 the total given duration.
A localized output is only supported for the units
CalendarUnit.YEARS, CalendarUnit.MONTHS,
CalendarUnit.WEEKS, CalendarUnit.DAYS and
all ClockUnit-units. This method performs an internal
normalization if any other unit is involved.
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 print(Duration<?> duration, TextWidth width, boolean printZero, int maxLength)
Formats given duration.
Like print(Duration, TextWidth), but offers the
option to limit the count of displayed duration items and also
to print items with zero amount. The first printed duration item
has always a non-zero amount however. Example:
Duration<?> dur =
Duration.ofZero().plus(1, DAYS).plus(4, ClockUnit.MINUTES);
System.out.println(
PrettyTime.of(Locale.FRANCE).print(dur, TextWidth.WIDE, true, 3));
// output: 1 jour, 0 heure et 4 minutes
duration - object representing a duration which might contain
several units and quantitieswidth - text width (ABBREVIATED as synonym for SHORT)printZero - determines if zero amounts shall be printed, toomaxLength - maximum count of displayed itemsIllegalArgumentException - if maxLength is smaller than 1public 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.