public class PrettyTime extends Object
Usage:
PrettyTime t = new PrettyTime();
String timestamp = t.format(new Date());
//result: moments from now
String timestamp = t.format(new Date(System.currentTimeMillis() + 1000 * 60 * 10));
//result: 10 minutes from now
| Constructor and Description |
|---|
PrettyTime()
Create a new
PrettyTime instance that will always use the current value of
System.currentTimeMillis() to represent the reference point for Date comparison, and will use
Locale.getDefault() as the selected Locale for language and dialect formatting. |
PrettyTime(Date reference)
Create a new
PrettyTime instance that will use the given Date timestamp to represent the reference
point for Date comparison, and will use Locale.getDefault() as the selected Locale for
language and dialect formatting. |
PrettyTime(Date reference,
Locale locale)
Construct a new
PrettyTime instance that will use the given Date timestamp to represent the
reference point for Date comparison, and will use the given Locale instead of the system default. |
PrettyTime(Locale locale)
Construct a new
PrettyTime instance that will always use the current value of
System.currentTimeMillis() to represent the reference point for Date comparison. |
public PrettyTime()
PrettyTime instance that will always use the current value of
System.currentTimeMillis() to represent the reference point for Date comparison, and will use
Locale.getDefault() as the selected Locale for language and dialect formatting.public PrettyTime(Date reference)
PrettyTime instance that will use the given Date timestamp to represent the reference
point for Date comparison, and will use Locale.getDefault() as the selected Locale for
language and dialect formatting. If the given Date is null, this instance will always use the
current value of System.currentTimeMillis() to represent the reference point for Date comparison.
See PrettyTime#setReference(Date timestamp).
public PrettyTime(Locale locale)
PrettyTime instance that will always use the current value of
System.currentTimeMillis() to represent the reference point for Date comparison. This instance
will use the given Locale instead of the system default. If the provided Locale is
null, Locale.getDefault() will be used.public PrettyTime(Date reference, Locale locale)
PrettyTime instance that will use the given Date timestamp to represent the
reference point for Date comparison, and will use the given Locale instead of the system default.
If the provided Locale is null, Locale.getDefault() will be used instead.
If the given Date is null, this instance will always use current value of
System.currentTimeMillis() will be used to represent the reference point for Date comparison.
See PrettyTime#setReference(Date timestamp).
public Duration approximateDuration(Date then)
Duration between the reference Date and given Date. If the given
Date is null, the current value of System.currentTimeMillis() will be used instead.
See PrettyTime#getReference().
public List<Duration> calculatePreciseDuration(Date then)
TimeUnit, the exact Duration represented by
the difference between the reference Date, and the given Date. If the given Date is
null, the current value of System.currentTimeMillis() will be used instead.
Note: Precision may be lost if no supplied TimeUnit is granular enough to represent the remainder
of time (in milliseconds).
then - The Date to be compared against the reference timestamp, or now if no reference
timestamp was providedList of Duration objects, from largest to smallest. Each element in the list
represents the approximate duration (number of times) that TimeUnit to fit into the previous
element's delta. The first element is the largest TimeUnit to fit within the total difference
between compared dates.public String format(Date then)
Date object. If the given Date is null, the current value of
System.currentTimeMillis() will be used instead.then - the Date to be formattedthenpublic String format(Calendar then)
Calendar object. If the given Calendar is null, the current value of
System.currentTimeMillis() will be used instead.then - the Calendar whose date is to be formattedthenpublic String format(Duration duration)
Duration object, using the TimeFormat specified by the TimeUnit contained
within. If the given Duration is null, the current value of
System.currentTimeMillis() will be used instead.duration - the Duration to be formatteddurationpublic String format(List<Duration> durations)
Duration objects, using the TimeFormat specified by the TimeUnit
contained within. Rounding rules are ignored for all but the last Duration element. If the given
Duration List is null or empty, the current value of
System.currentTimeMillis() will be used instead.durations - the Durations to be formatteddurationspublic String formatUnrounded(Date then)
Date object. Rounding rules are ignored. If the given Date is null,
the current value of System.currentTimeMillis() will be used instead.then - the Date to be formattedthenpublic String formatUnrounded(Calendar then)
Calendar object. This method applies the approximateDuration(Date)
method to perform its calculation. Rounding rules are ignored. If the given Calendar is null,
the current value of System.currentTimeMillis() will be used instead.then - the Calendar whose date is to be formattedthenpublic String formatUnrounded(Duration duration)
Duration object, using the TimeFormat specified by the TimeUnit contained
within. Rounding rules are ignored. If the given Duration is null, the current value of
System.currentTimeMillis() will be used instead.duration - the Duration to be formatteddurationpublic String formatUnrounded(List<Duration> durations)
Duration objects, using the TimeFormat specified by the TimeUnit
contained within. Rounding rules are ignored. If the given Duration List is null or
empty, the current value of System.currentTimeMillis() will be used instead.durations - the Durations to be formatteddurationspublic String formatDuration(Date then)
Date and return a non-relative (not decorated with past or future tense) String
for the approximate duration of its difference between the reference Date. If the given Date is
null, the current value of System.currentTimeMillis() will be used instead.
then - the date to be formattedDatepublic String formatDuration(Calendar then)
Calendar and return a non-relative (not decorated with past or future tense)
String for the approximate duration of its difference between the reference Date. If the given
Calendar is null, the current value of System.currentTimeMillis() will be used
instead.
then - the date to be formattedDatepublic String formatDuration(Duration duration)
Duration and return a non-relative (not decorated with past or future tense)
String for the approximate duration of the difference between the reference Date and the given
Duration. If the given Duration is null, the current value of
System.currentTimeMillis() will be used instead.duration - the duration to be formattedDurationpublic String formatDuration(List<Duration> durations)
Duration List and return a non-relative (not decorated with past or future tense)
String for the approximate duration of its difference between the reference Date. If the given
Duration is null, the current value of System.currentTimeMillis() will be used
instead.duration - the duration to be formattedDurationpublic String formatDurationUnrounded(Date then)
Date and return a non-relative (not decorated with past or future tense) String
for the approximate duration of its difference between the reference Date. Rounding rules are ignored. If
the given Date is null, the current value of System.currentTimeMillis() will be used
instead.
then - the date to be formattedDatepublic String formatDurationUnrounded(Calendar then)
Calendar and return a non-relative (not decorated with past or future tense)
String for the approximate duration of its difference between the reference Date. Rounding rules
are ignored. If the given Calendar is null, the current value of
System.currentTimeMillis() will be used instead.
then - the date to be formattedDatepublic String formatDurationUnrounded(Duration duration)
Duration and return a non-relative (not decorated with past or future tense)
String for the approximate duration of its difference between the reference Date. Rounding rules
are ignored. If the given Duration is null, the current value of
System.currentTimeMillis() will be used instead.duration - the duration to be formattedDurationpublic String formatDurationUnrounded(List<Duration> durations)
Duration List and return a non-relative (not decorated with past or future tense)
String for the approximate duration of its difference between the reference Date. Rounding rules
are ignored. If the given Duration is null, the current value of
System.currentTimeMillis() will be used instead.duration - the duration to be formattedDurationpublic TimeFormat getFormat(TimeUnit unit)
public Date getReference()
public PrettyTime setReference(Date timestamp)
Date. If null, PrettyTime will always use the current value of
System.currentTimeMillis() as the reference Date.
If the Date formatted is before the reference Date, the format command will produce a
String that is in the past tense. If the Date formatted is after the reference Date, the
format command will produce a String that is in the future tense.
public PrettyTime registerUnit(TimeUnit unit, TimeFormat format)
TimeUnit and corresponding TimeFormat instance to be used in calculations. If
an entry already exists for the given TimeUnit, its TimeFormat will be overwritten with the given
TimeFormat. (TimeUnit and TimeFormat must not be null.)public <UNIT extends TimeUnit> TimeFormat removeUnit(Class<UNIT> unitType)
TimeUnit type. This effectively de-registers the TimeUnit so it
will not be used in formatting. Returns the TimeFormat that was removed, or null if no unit
of the given type was registered.public TimeFormat removeUnit(TimeUnit unit)
TimeUnit. This effectively de-registers the TimeUnit so it will
not be used in formatting. Returns the TimeFormat that was removed, or null if no such unit was
registered.public Locale getLocale()
Locale for this PrettyTime object.public PrettyTime setLocale(Locale locale)
Locale for this PrettyTime object. This may be an expensive operation, since this
operation calls TimeUnit#setLocale(Locale) for each TimeUnit in getUnits().Copyright © 2015 OCPsoft. All Rights Reserved.