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)
Formats the total given duration.
|
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 |
print(TemporalAmount threeten)
Short-cut for
print(Duration.from(threeten)). |
String |
print(TemporalAmount threeten,
TextWidth width)
Short-cut for
print(Duration.from(threeten), width). |
String |
print(TemporalAmount threeten,
TextWidth width,
boolean printZero,
int maxLength)
Short-cut for
print(Duration.from(threeten), width, printZero, maxLength). |
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,
Timezone tz,
TimeUnit precision)
Formats given time point relative to the current time of
getReferenceClock()
as duration in given 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. |
String |
printRelativeInStdTimezone(UnixTime moment)
Formats given time point relative to the current time of
getReferenceClock() as duration in at most second
precision or less - using the system timezone. |
String |
printRelativeOrDateTime(UnixTime moment,
Timezone tz,
TimeUnit precision,
CalendarUnit maxRelativeUnit,
TemporalFormatter<Moment> formatter)
Formats given time point relative to the current time of
getReferenceClock()
as duration in given precision or as absolute date-time. |
String |
printRelativeOrDateTime(UnixTime moment,
Timezone tz,
TimeUnit precision,
long maxdelta,
TemporalFormatter<Moment> formatter)
Formats given time point relative to the current time of
getReferenceClock()
as duration in given precision or as absolute date-time. |
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 |
withMinusSign(String minusSign)
Defines the localized minus sign.
|
PrettyTime |
withReferenceClock(TimeSource<?> clock)
Yields a changed copy of this instance with given reference
clock.
|
PrettyTime |
withShortStyle()
Mandates the use of abbreviations as default style.
|
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 for example in arabic locales the digit can also be the char
U+0660. By default Time4J will try to use the configuration
of the module i18n or else the JDK-setting. This method can override
it however.
zeroDigit - localized zero digitDecimalFormatSymbols.getZeroDigit(),
NumberSymbolProvider.getZeroDigit(Locale)public PrettyTime withMinusSign(String minusSign)
Defines the localized minus sign.
In most languages the minus sign is just U+002D. By default
Time4J will try to use the configuration of the module i18n or else the
JDK-setting. This method can override it however. Especially for arabic,
it might make sense to first add a unicode marker (either LRM
U+200E or RLM U+200F) in front of the minus sign
in order to control the orientation in right-to-left-style.
minusSign - localized minus sign (possibly with unicode markers)DecimalFormatSymbols.getMinusSign(),
NumberSymbolProvider.getMinusSign(Locale)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 PrettyTime withShortStyle()
Mandates the use of abbreviations as default style.
All print()-methods with explicit TextWidth-parameters will ignore this
setting however. This method is mainly relevant for printing relative times.
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)
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: This method uses full words by default. If
withShortStyle() is called then abbreviations will be used.
duration - object representing a duration which might contain several units and quantitiespublic String print(TemporalAmount threeten)
Short-cut for print(Duration.from(threeten)).
threeten - object representing a duration which might contain several units and quantitiesIllegalArgumentException - in case of conversion failuresprint(Duration)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.
duration - object representing a duration which might contain
several units and quantitieswidth - text width (ABBREVIATED as synonym for SHORT)public String print(TemporalAmount threeten, TextWidth width)
Short-cut for print(Duration.from(threeten), width).
threeten - object representing a duration which might contain several units and quantitieswidth - text width (ABBREVIATED as synonym for SHORT)IllegalArgumentException - in case of conversion failuresprint(Duration, TextWidth)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 print(TemporalAmount threeten, TextWidth width, boolean printZero, int maxLength)
Short-cut for print(Duration.from(threeten), width, printZero, maxLength).
threeten - 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 1 or in case of conversion failuresprint(Duration, TextWidth, boolean, int)public String printRelativeInStdTimezone(UnixTime moment)
Formats given time point relative to the current time of
getReferenceClock() as duration in at most second
precision or less - using the system timezone.
moment - relative time pointprintRelative(UnixTime, Timezone, TimeUnit)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.
Example how to query for a coming leap second:
TimeSource<?> clock = () -> PlainTimestamp.of(2015, 6, 30, 23, 59, 54).atUTC();
String remainingDurationInRealSeconds =
PrettyTime.of(Locale.ENGLISH).withReferenceClock(clock).printRelative(
PlainTimestamp.of(2015, 7, 1, 0, 0, 0).atUTC(),
ZonalOffset.UTC);
System.out.println(remainingDurationInRealSeconds); // in 7 seconds
moment - relative time pointtzid - time zone id for translating to a local durationprintRelative(UnixTime, Timezone, TimeUnit)public 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 durationprintRelative(UnixTime, Timezone, TimeUnit)public String printRelative(UnixTime moment, Timezone tz, TimeUnit precision)
Formats given time point relative to the current time of getReferenceClock()
as duration in given precision or less.
If day precision is given then output like "today", "yesterday" or "tomorrow" is possible. Example:
TimeSource<?> clock = () -> PlainTimestamp.of(2015, 8, 1, 10, 24, 5).atUTC();
String durationInDays =
PrettyTime.of(Locale.GERMAN).withReferenceClock(clock).printRelative(
PlainTimestamp.of(2015, 8, 1, 17, 0).atUTC(),
Timezone.of(EUROPE.BERLIN),
TimeUnit.DAYS);
System.out.println(durationInDays); // heute (german word for today)
moment - relative time pointtz - time zone for translating to a local durationprecision - maximum precision of relative time (not more than seconds)public String printRelativeOrDateTime(UnixTime moment, Timezone tz, TimeUnit precision, long maxdelta, TemporalFormatter<Moment> formatter)
Formats given time point relative to the current time of getReferenceClock()
as duration in given precision or as absolute date-time.
moment - relative time pointtz - time zone for translating to a local durationprecision - maximum precision of relative time (not more than seconds)maxdelta - maximum deviation of given moment from clock in posix seconds for relative printingformatter - used for printing absolute time if the deviation is bigger than maxdeltapublic String printRelativeOrDateTime(UnixTime moment, Timezone tz, TimeUnit precision, CalendarUnit maxRelativeUnit, TemporalFormatter<Moment> formatter)
Formats given time point relative to the current time of getReferenceClock()
as duration in given precision or as absolute date-time.
moment - time point whose deviation from clock is to be printedtz - time zone for translating to a local durationprecision - maximum precision of relative time (not more than seconds)maxRelativeUnit - maximum time unit which will still be printed in a relative wayformatter - used for printing absolute time if the leading unit is bigger than maxRelativeUnitCopyright © 2014–2015. All rights reserved.