Class PrettyTime
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.
- Since:
- 1.2
- Author:
- Meno Hochschild
-
Method Summary
Modifier and TypeMethodDescriptionGets the language of this instance.TimeSource<?>
Yields the reference clock for formatting of relative times.static PrettyTime
Gets an instance ofPrettyTime
for given language, possibly cached.print(long amount, CalendarUnit unit, TextWidth width)
Formats given duration in calendar units.Formats given duration in clock units.print(TemporalAmount threeten)
Short-cut forprint(Duration.from(threeten))
.print(TemporalAmount threeten, TextWidth width)
Short-cut forprint(Duration.from(threeten), width)
.print(TemporalAmount threeten, TextWidth width, boolean printZero, int maxLength)
Short-cut forprint(Duration.from(threeten), width, printZero, maxLength)
.Formats the total given duration.Formats the total given duration.Formats given duration.Obtains the localized word for given last day-of-week if available.Obtains the localized word for given next day-of-week if available.printRelative(Instant instant, ZoneId zoneId)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in at most second precision or less.printRelative(ZonedDateTime zdt)
Formats given threeten object relative to the current time ofgetReferenceClock()
as duration in at most second precision or less.printRelative(UnixTime moment, String tzid)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in at most second precision or less.printRelative(UnixTime moment, Timezone tz, TimeUnit precision)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in given precision or less.printRelative(UnixTime moment, TZID tzid)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in at most second precision or less.printRelativeInStdTimezone(UnixTime moment)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in at most second precision or less - using the system timezone.printRelativeOrDate(PlainDate date, TZID tzid, CalendarUnit maxRelativeUnit, TemporalFormatter<PlainDate> formatter)
Formats given date relative to the current date ofgetReferenceClock()
as duration or as absolute date.printRelativeOrDateTime(UnixTime moment, Timezone tz, TimeUnit precision, long maxdelta, TemporalFormatter<Moment> formatter)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in given precision or as absolute date-time.printRelativeOrDateTime(UnixTime moment, Timezone tz, TimeUnit precision, CalendarUnit maxRelativeUnit, TemporalFormatter<Moment> formatter)
Formats given time point relative to the current time ofgetReferenceClock()
as duration in given precision or as absolute date-time.Determines the localized word for "today".Determines the localized word for "tomorrow".Determines the localized word for "yesterday".withDefaultListSeparator(String separator)
Mandates the use of given default list separator.withEmptyUnit(CalendarUnit emptyUnit)
Defines the time unit used for formatting an empty duration.withEmptyUnit(ClockUnit emptyUnit)
Defines the time unit used for formatting an empty duration.withLastListSeparator(String separator)
Mandates the use of given list separator at the last position.withMinusSign(String minusSign)
Defines the localized minus sign.withNumerals(NumberSystem numsys)
Defines the given number system for representing amounts.withReferenceClock(TimeSource<?> clock)
Yields a changed copy of this instance with given reference clock.Mandates the use of abbreviations as default style.Determines that weeks will always be normalized to days.withZeroDigit(char zeroDigit)
Defines the localized zero digit.withZeroDigit(NumberSystem numberSystem)
Deprecated.
-
Method Details
-
of
Gets an instance of
PrettyTime
for given language, possibly cached.- Parameters:
locale
- the language an instance is searched for- Returns:
- pretty time object for formatting durations or relative time
- Since:
- 1.2
-
getLocale
Gets the language of this instance.
- Returns:
- language
- Since:
- 1.2
-
getReferenceClock
Yields the reference clock for formatting of relative times.
- Returns:
- reference clock or system clock if not yet specified
- Since:
- 1.2
- See Also:
withReferenceClock(TimeSource)
,printRelative(UnixTime, TZID)
,printRelative(UnixTime, String)
-
withReferenceClock
Yields a changed copy of this instance with given reference clock.
- Parameters:
clock
- new reference clock- Returns:
- new instance of
PrettyTime
with changed reference clock - Since:
- 1.2
- See Also:
getReferenceClock()
,printRelative(UnixTime, TZID)
,printRelative(UnixTime, String)
-
withNumerals
Defines the given number system for representing amounts.
If decimal then the zero digit will also be adjusted appropriately.
- Parameters:
numsys
- number system- Returns:
- changed copy of this instance
- Since:
- 5.9
- See Also:
withZeroDigit(char)
-
withZeroDigit
Deprecated.Use eitherwithZeroDigit(char)
orwithNumerals(NumberSystem)
Defines the localized zero digit based on given decimal number system.
- Parameters:
numberSystem
- decimal number system- Returns:
- changed copy of this instance
- Throws:
IllegalArgumentException
- if the number system is not decimal- Since:
- 3.24/4.20
- See Also:
withZeroDigit(char)
-
withZeroDigit
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.Note: This method can only produce sensible results if all digits 0-9 can be mapped to code points starting with given zero digit char.
- Parameters:
zeroDigit
- localized zero digit- Returns:
- changed copy of this instance
- Since:
- 1.2
- See Also:
DecimalFormatSymbols.getZeroDigit()
,NumberSymbolProvider.getZeroDigit(Locale)
-
withMinusSign
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 LRMU+200E
or RLMU+200F
) in front of the minus sign in order to control the orientation in right-to-left-style.- Parameters:
minusSign
- localized minus sign (possibly with unicode markers)- Returns:
- changed copy of this instance
- Since:
- 2.1
- See Also:
DecimalFormatSymbols.getMinusSign()
,NumberSymbolProvider.getMinusSign(Locale)
-
withEmptyUnit
Defines the time unit used for formatting an empty duration.
Time4J uses seconds as default. This method can override the default however.
- Parameters:
emptyUnit
- time unit for usage in an empty duration- Returns:
- changed copy of this instance
- Since:
- 1.2
- See Also:
print(Duration, TextWidth)
-
withEmptyUnit
Defines the time unit used for formatting an empty duration.
Time4J uses seconds as default. This method can override the default however.
- Parameters:
emptyUnit
- time unit for usage in an empty duration- Returns:
- changed copy of this instance
- Since:
- 1.2
- See Also:
print(Duration, TextWidth)
-
withWeeksToDays
Determines that weeks will always be normalized to days.
- Returns:
- changed copy of this instance
- Since:
- 2.0
-
withShortStyle
Mandates the use of abbreviations as default style.
All
print()
-methods with explicitTextWidth
-parameters will ignore this setting however. This method is mainly relevant for printing relative times.- Returns:
- changed copy of this instance
- Since:
- 3.6/4.4
-
withDefaultListSeparator
Mandates the use of given default list separator.
Usually the locale specifies any list patterns for printing durations. However, this method allows customized list separators. Example:
assertThat( PrettyTime.of(Locale.US) .withDefaultListSeparator(" | ") .withLastListSeparator(" + ") .print(Duration.ofCalendarUnits(1, 2, 3), TextWidth.WIDE), is("1 year | 2 months + 3 days"));
- Parameters:
separator
- the separator characters between any duration items- Returns:
- changed copy of this instance
- Since:
- 5.2
- See Also:
withLastListSeparator(String)
-
withLastListSeparator
Mandates the use of given list separator at the last position.
Important: A default list separator must be specified otherwise this method will have no effect.
- Parameters:
separator
- the separator characters between the last two duration items- Returns:
- changed copy of this instance
- Since:
- 5.2
- See Also:
withDefaultListSeparator(String)
-
printYesterday
Determines the localized word for "yesterday".
- Returns:
- String
- Since:
- 3.43/4.38
-
printToday
Determines the localized word for "today".
- Returns:
- String
- Since:
- 3.24/4.20
-
printTomorrow
Determines the localized word for "tomorrow".
- Returns:
- String
- Since:
- 3.43/4.38
-
printLast
Obtains the localized word for given last day-of-week if available.
- Parameters:
weekday
- the last day of week- Returns:
- localized word, maybe empty
- Since:
- 5.1
-
printNext
Obtains the localized word for given next day-of-week if available.
- Parameters:
weekday
- the next day of week- Returns:
- localized word, maybe empty
- Since:
- 5.1
-
print
Formats given duration in calendar units.
Note: Millennia, centuries and decades are automatically normalized to years while quarter-years are normalized to months.
- Parameters:
amount
- count of units (quantity)unit
- calendar unitwidth
- text width (ABBREVIATED as synonym for SHORT)- Returns:
- formatted output
- Since:
- 1.2
- See Also:
print(Duration, TextWidth)
-
print
Formats given duration in clock units.
- Parameters:
amount
- count of units (quantity)unit
- clock unitwidth
- text width (ABBREVIATED as synonym for SHORT)- Returns:
- formatted output
- Since:
- 1.2
- See Also:
print(Duration, TextWidth)
-
print
Formats the total given duration.
A localized output is only supported for the units
CalendarUnit.YEARS
,CalendarUnit.MONTHS
,CalendarUnit.WEEKS
,CalendarUnit.DAYS
and allClockUnit
-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.- Parameters:
duration
- object representing a duration which might contain several units and quantities- Returns:
- formatted list output
- Since:
- 3.6/4.4
-
print
Short-cut for
print(Duration.from(threeten))
.- Parameters:
threeten
- object representing a duration which might contain several units and quantities- Returns:
- formatted list output in normalized form
- Throws:
IllegalArgumentException
- in case of conversion failures- Since:
- 4.4
- See Also:
print(Duration)
-
print
Formats the total given duration.
A localized output is only supported for the units
CalendarUnit.YEARS
,CalendarUnit.MONTHS
,CalendarUnit.WEEKS
,CalendarUnit.DAYS
and allClockUnit
-units. This method performs an internal normalization if any other unit is involved.- Parameters:
duration
- object representing a duration which might contain several units and quantitieswidth
- text width (ABBREVIATED as synonym for SHORT)- Returns:
- formatted list output
- Since:
- 1.2
-
print
Short-cut for
print(Duration.from(threeten), width)
.- Parameters:
threeten
- object representing a duration which might contain several units and quantitieswidth
- text width (ABBREVIATED as synonym for SHORT)- Returns:
- formatted list output in normalized form
- Throws:
IllegalArgumentException
- in case of conversion failures- Since:
- 4.0
- See Also:
print(Duration, TextWidth)
-
print
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
- Parameters:
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 items- Returns:
- formatted list output
- Throws:
IllegalArgumentException
- if maxLength is smaller than1
- Since:
- 2.0
-
print
Short-cut for
print(Duration.from(threeten), width, printZero, maxLength)
.- Parameters:
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 items- Returns:
- formatted list output in normalized form
- Throws:
IllegalArgumentException
- if maxLength is smaller than1
or in case of conversion failures- Since:
- 4.0
- See Also:
print(Duration, TextWidth, boolean, int)
-
printRelativeInStdTimezone
Formats given time point relative to the current time of
getReferenceClock()
as duration in at most second precision or less - using the system timezone.- Parameters:
moment
- relative time point- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 3.4/4.3
- See Also:
printRelative(UnixTime, Timezone, TimeUnit)
-
printRelative
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
- Parameters:
moment
- relative time pointtzid
- time zone id for translating to a local duration- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 1.2
- See Also:
printRelative(UnixTime, Timezone, TimeUnit)
-
printRelative
Formats given time point relative to the current time of
getReferenceClock()
as duration in at most second precision or less.- Parameters:
moment
- relative time pointtzid
- time zone id for translating to a local duration- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 1.2
- See Also:
printRelative(UnixTime, Timezone, TimeUnit)
-
printRelative
Formats given threeten object relative to the current time of
getReferenceClock()
as duration in at most second precision or less.- Parameters:
zdt
- relative time- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 4.8
- See Also:
printRelative(Instant, ZoneId)
-
printRelative
Formats given time point relative to the current time of
getReferenceClock()
as duration in at most second precision or less.- Parameters:
instant
- relative time pointzoneId
- time zone id for translating to a local duration- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 4.8
- See Also:
printRelative(UnixTime, Timezone, TimeUnit)
-
printRelative
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", "tomorrow" or "last Wednesday" 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)
- Parameters:
moment
- relative time pointtz
- time zone for translating to a local durationprecision
- maximum precision of relative time (not more than seconds)- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 3.6/4.4
-
printRelativeOrDateTime
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.If the calculated duration in seconds is bigger than
maxdelta
then the absolute date-time will be printed else a relative expression will be used.- Parameters:
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 maxdelta- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 3.6/4.4
-
printRelativeOrDateTime
public 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.- Parameters:
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 maxRelativeUnit- Returns:
- formatted output of relative time, either in past or in future
- Since:
- 3.6/4.4
-
printRelativeOrDate
public String printRelativeOrDate(PlainDate date, TZID tzid, CalendarUnit maxRelativeUnit, TemporalFormatter<PlainDate> formatter)Formats given date relative to the current date of
getReferenceClock()
as duration or as absolute date.- Parameters:
date
- calendar date whose deviation from clock is to be printedtzid
- time zone identifier for getting current reference datemaxRelativeUnit
- maximum calendar unit which will still be printed in a relative wayformatter
- used for printing absolute date if the leading unit is bigger than maxRelativeUnit- Returns:
- formatted output of relative date, either in past or in future
- Since:
- 3.7/4.5
-
withZeroDigit(char)
orwithNumerals(NumberSystem)