@ValidScope(value="application") public class ComparisonDateTool extends DateTool
Tool for comparing Date
and Calendar
values
in Velocity templates. This is a subclass of DateTool
and thus provides all the functionality of that tool and
augments it with the ability to find the relationship between
any date and the current date, or between any two dates.
This comparison can result in either a textual representation
of the relationship (e.g. "3 weeks, 2 days ago", "tomorrow", or
"3 hrs away") or the value of a specific time unit may be requested.
When using the textual representations, you can configure the
tool to use alternate resource bundles and to skip over units
you do not want to be included.
Example of formatting the "current" date: $date.whenIs('2005-07-04') -> 1 year ago $date.whenIs('2007-02-15').full -> 1 year 32 weeks 2 days 17 hours 38 minutes 44 seconds 178 milliseconds ago $date.whenIs('2007-02-15').days -> -730 $date.whenIs($date.calendar) -> now $date.whenIs('2005-07-04', '2005-07-04') -> same time $date.difference('2005-07-04','2005-07-04') -> 0 milliseconds $date.difference('2005-07-04','2007-02-15').abbr -> 1 yr Example tools.xml config (if you want to use this with VelocityView): <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.ComparisonDateTool" format="yyyy-MM-dd" depth="1" skip="month,week,millisecond" bundle="org.apache.velocity.tools.generic.times"/> </toolbox> </tools>
Modifier and Type | Class and Description |
---|---|
class |
ComparisonDateTool.Comparison |
Modifier and Type | Field and Description |
---|---|
protected static String |
ABBR_SUFFIX |
protected static String |
AFTER_KEY |
protected static String |
BEFORE_KEY |
static String |
BUNDLE_NAME_KEY
The key used for specifying a default locale via toolbox params.
|
protected static String |
CURRENT_PREFIX |
protected static int |
CURRENT_TYPE |
protected static String |
DAY_KEY |
static String |
DEFAULT_BUNDLE_NAME
The default path of the relative format resource bundles.
|
static String |
DEPTH_KEY
The key used for specifying a different default depth via toolbox params.
|
protected static int |
DIFF_TYPE |
protected static String |
EQUAL_KEY |
protected static String |
HOUR_KEY |
static long |
MILLIS_PER_DAY
The number of milliseconds in a day.
|
static long |
MILLIS_PER_HOUR
The number of milliseconds in an hour.
|
static long |
MILLIS_PER_MINUTE
The number of millseconds in a minute.
|
static long |
MILLIS_PER_MONTH
An approximation of the number of milliseconds in a month.
|
static long |
MILLIS_PER_SECOND
The number of milliseconds in a second.
|
static long |
MILLIS_PER_WEEK
The number of milliseconds in a week.
|
static long |
MILLIS_PER_YEAR
An approximation of the number of milliseconds in a year.
|
protected static String |
MILLISECOND_KEY |
protected static String |
MINUTE_KEY |
protected static String |
MONTH_KEY |
protected static String |
ONE_DAY_SUFFIX |
protected static String |
PLURAL_SUFFIX |
protected static int |
RELATIVE_TYPE |
protected static String |
SECOND_KEY |
static String |
SKIPPED_UNITS_KEY
The key used for specifying time units to be skipped over.
|
protected static Map |
TIME_UNITS
Array of all time unit message keys to their millisecond conversion factor.
|
protected static String |
WEEK_KEY |
protected static String |
YEAR_KEY |
protected static String |
ZERO_KEY |
TIMEZONE_KEY
DEFAULT_FORMAT, FORMAT_KEY
DEFAULT_LOCALE
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
ComparisonDateTool() |
Modifier and Type | Method and Description |
---|---|
protected ComparisonDateTool.Comparison |
compare(Object now,
Object then,
int type)
Internal comparison method.
|
protected void |
configure(ValueParser values)
Calls the superclass implementation, then looks for a bundle name
and any time units to be skipped.
|
ComparisonDateTool.Comparison |
difference(Object now,
Object then)
Returns a
ComparisonDateTool.Comparison between the result of
the second specified date and the first specified date. |
protected String |
getText(String key,
Locale locale)
Retrieves the specified text resource.
|
static long |
toDays(long ms)
Returns the number of whole Days in the specified number of milliseconds.
|
static long |
toHours(long ms)
Returns the number of whole Hours in the specified number of milliseconds.
|
static long |
toMinutes(long ms)
Returns the number of whole Minutes in the specified number of milliseconds.
|
static long |
toMonths(long ms)
Returns the number of whole Months in the specified number of milliseconds.
|
static long |
toSeconds(long ms)
Returns the number of whole Seconds in the specified number of milliseconds.
|
protected String |
toString(long diff,
int maxUnitDepth,
boolean abbreviate,
Locale locale)
Converts the specified positive duration of milliseconds into larger
units up to the specified number of positive units, beginning with the
largest positive unit.
|
protected String |
toString(long ms,
int type,
int depth,
boolean abbr,
Locale loc) |
static long |
toWeeks(long ms)
Returns the number of whole Weeks in the specified number of milliseconds.
|
static long |
toYears(long ms)
Returns the number of whole Years in the specified number of milliseconds.
|
ComparisonDateTool.Comparison |
whenIs(Object then)
Returns a
ComparisonDateTool.Comparison between the result of
DateTool.getCalendar() and the specified date. |
ComparisonDateTool.Comparison |
whenIs(Object now,
Object then)
Returns a
ComparisonDateTool.Comparison between the second specified date
and the first specified date. |
format, format, format, format, format, format, format, get, get, getCalendar, getDate, getDateFormat, getDateFormat, getDay, getDay, getMonth, getMonth, getSystemCalendar, getSystemDate, getSystemTime, getTimeZone, getValue, getValue, getValue, getYear, getYear, setTimeZone, toCalendar, toCalendar, toDate, toDate, toDate, toDate, toLocalizedPattern, toString
getFormat, setFormat
getLocale, setLocale, toLocale
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final long MILLIS_PER_SECOND
public static final long MILLIS_PER_MINUTE
public static final long MILLIS_PER_HOUR
public static final long MILLIS_PER_DAY
public static final long MILLIS_PER_WEEK
public static final long MILLIS_PER_MONTH
public static final long MILLIS_PER_YEAR
public static final String BUNDLE_NAME_KEY
public static final String DEPTH_KEY
public static final String SKIPPED_UNITS_KEY
public static final String DEFAULT_BUNDLE_NAME
protected static final String MILLISECOND_KEY
protected static final String SECOND_KEY
protected static final String MINUTE_KEY
protected static final String HOUR_KEY
protected static final String DAY_KEY
protected static final String WEEK_KEY
protected static final String MONTH_KEY
protected static final String YEAR_KEY
protected static final Map TIME_UNITS
protected static final String CURRENT_PREFIX
protected static final String AFTER_KEY
protected static final String BEFORE_KEY
protected static final String EQUAL_KEY
protected static final String ZERO_KEY
protected static final String ABBR_SUFFIX
protected static final String ONE_DAY_SUFFIX
protected static final String PLURAL_SUFFIX
protected static final int CURRENT_TYPE
protected static final int RELATIVE_TYPE
protected static final int DIFF_TYPE
protected void configure(ValueParser values)
protected String getText(String key, Locale locale)
key
- keylocale
- localepublic static long toYears(long ms)
ms
- millisecondspublic static long toMonths(long ms)
ms
- millisecondspublic static long toWeeks(long ms)
ms
- millisecondspublic static long toDays(long ms)
ms
- millisecondspublic static long toHours(long ms)
ms
- millisecondspublic static long toMinutes(long ms)
ms
- millisecondspublic static long toSeconds(long ms)
ms
- millisecondspublic ComparisonDateTool.Comparison whenIs(Object then)
ComparisonDateTool.Comparison
between the result of
DateTool.getCalendar()
and the specified date. The default
rendering of that Comparison will be the largest unit difference
between the dates followed by a description of their relative position.then
- The date in questionComparisonDateTool.Comparison
objectpublic ComparisonDateTool.Comparison whenIs(Object now, Object then)
ComparisonDateTool.Comparison
between the second specified date
and the first specified date. The default
rendering of that Comparison will be the largest unit difference
between the dates followed by a description of their relative position.now
- The date to use as representative of "now"then
- The date in questionComparisonDateTool.Comparison
objectpublic ComparisonDateTool.Comparison difference(Object now, Object then)
ComparisonDateTool.Comparison
between the result of
the second specified date and the first specified date. The default
rendering of that Comparison will be the largest unit difference
between the dates.now
- The date to use as representative of "now"then
- The secondary dateComparisonDateTool.Comparison
objectprotected ComparisonDateTool.Comparison compare(Object now, Object then, int type)
now
- The date to use as representative of "now"then
- The secondary datetype
- Difference typeComparisonDateTool.Comparison
objectprotected String toString(long ms, int type, int depth, boolean abbr, Locale loc)
ms
- The time in millisecondstype
- Whether the time should be represented as relative to "now",
relative to some other time, or as a mere difference.depth
- The maximum number of units deep to showabbr
- Whether the units should be abbreviated or notloc
- The locale to be used when looking up resourcesprotected String toString(long diff, int maxUnitDepth, boolean abbreviate, Locale locale)
toString(181453, 3, false, null)
will return
"3 minutes 1 second 453 milliseconds",
toString(181453, 2, false, null)
will return
"3 minutes 1 second", and
toString(180000, 2, true, null)
will return
"3 min".diff
- millisecondsmaxUnitDepth
- maximum unit depthabbreviate
- whether to abbreviate unit nameslocale
- locale to useCopyright © 2002–2018 The Apache Software Foundation. All rights reserved.