Class ComparisonDateTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.FormatConfig
org.apache.velocity.tools.generic.DateTool
org.apache.velocity.tools.generic.ComparisonDateTool
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>
- Since:
- VelocityTools 1.4
- Version:
- $Revision: 595822 $ $Date: 2006-04-04 12:35:17 -0700 (Tue, 04 Apr 2006) $
- Author:
- Nathan Bubna, Chris Townsen
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final String
protected static final String
protected static final String
static final String
The key used for specifying a default locale via toolbox params.private String
protected static final String
protected static final int
protected static final String
static final String
The default path of the relative format resource bundles.private ResourceBundle
private int
static final String
The key used for specifying a different default depth via toolbox params.protected static final int
protected static final String
protected static final String
static final long
The number of milliseconds in a day.static final long
The number of milliseconds in an hour.static final long
The number of millseconds in a minute.static final long
An approximation of the number of milliseconds in a month.static final long
The number of milliseconds in a second.static final long
The number of milliseconds in a week.static final long
An approximation of the number of milliseconds in a year.protected static final String
protected static final String
protected static final String
protected static final String
protected static final String
protected static final int
protected static final String
static final String
The key used for specifying time units to be skipped over.protected static final Map
Array of all time unit message keys to their millisecond conversion factor.private Map
protected static final String
protected static final String
protected static final String
Fields inherited from class org.apache.velocity.tools.generic.DateTool
DEFAULT_FORMAT_KEY, DEFAULT_LOCALE_KEY, TIMEZONE_KEY
Fields inherited from class org.apache.velocity.tools.generic.FormatConfig
DEFAULT_FORMAT, FORMAT_KEY
Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ComparisonDateTool.Comparison
protected void
configure
(ValueParser values) Calls the superclass implementation, then looks for a bundle name and any time units to be skipped.difference
(Object now, Object then) Returns aComparisonDateTool.Comparison
between the result of the second specified date and the first specified date.protected String
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
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
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.Returns aComparisonDateTool.Comparison
between the result ofDateTool.getCalendar()
and the specified date.Returns aComparisonDateTool.Comparison
between the second specified date and the first specified date.Methods inherited from class org.apache.velocity.tools.generic.DateTool
format, format, format, format, format, format, format, get, get, getCalendar, getDate, getDateFormat, getDateFormat, getDateFormat, getDay, getDay, getMonth, getMonth, getStyleAsInt, getSystemCalendar, getSystemDate, getSystemTime, getTimeZone, getValue, getValue, getValue, getYear, getYear, setTimeZone, toCalendar, toCalendar, toDate, toDate, toDate, toDate, toString
Methods inherited from class org.apache.velocity.tools.generic.FormatConfig
getFormat, setFormat
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
Field Details
-
MILLIS_PER_SECOND
public static final long MILLIS_PER_SECONDThe number of milliseconds in a second.- See Also:
-
MILLIS_PER_MINUTE
public static final long MILLIS_PER_MINUTEThe number of millseconds in a minute.- See Also:
-
MILLIS_PER_HOUR
public static final long MILLIS_PER_HOURThe number of milliseconds in an hour.- See Also:
-
MILLIS_PER_DAY
public static final long MILLIS_PER_DAYThe number of milliseconds in a day.- See Also:
-
MILLIS_PER_WEEK
public static final long MILLIS_PER_WEEKThe number of milliseconds in a week.- See Also:
-
MILLIS_PER_MONTH
public static final long MILLIS_PER_MONTHAn approximation of the number of milliseconds in a month.- See Also:
-
MILLIS_PER_YEAR
public static final long MILLIS_PER_YEARAn approximation of the number of milliseconds in a year.- See Also:
-
BUNDLE_NAME_KEY
The key used for specifying a default locale via toolbox params.- See Also:
-
DEPTH_KEY
The key used for specifying a different default depth via toolbox params.- See Also:
-
SKIPPED_UNITS_KEY
The key used for specifying time units to be skipped over.- See Also:
-
DEFAULT_BUNDLE_NAME
The default path of the relative format resource bundles.- See Also:
-
MILLISECOND_KEY
- See Also:
-
SECOND_KEY
- See Also:
-
MINUTE_KEY
- See Also:
-
HOUR_KEY
- See Also:
-
DAY_KEY
- See Also:
-
WEEK_KEY
- See Also:
-
MONTH_KEY
- See Also:
-
YEAR_KEY
- See Also:
-
TIME_UNITS
Array of all time unit message keys to their millisecond conversion factor. -
CURRENT_PREFIX
- See Also:
-
AFTER_KEY
- See Also:
-
BEFORE_KEY
- See Also:
-
EQUAL_KEY
- See Also:
-
ZERO_KEY
- See Also:
-
ABBR_SUFFIX
- See Also:
-
ONE_DAY_SUFFIX
- See Also:
-
PLURAL_SUFFIX
- See Also:
-
CURRENT_TYPE
protected static final int CURRENT_TYPE- See Also:
-
RELATIVE_TYPE
protected static final int RELATIVE_TYPE- See Also:
-
DIFF_TYPE
protected static final int DIFF_TYPE- See Also:
-
bundleName
-
defaultBundle
-
timeUnits
-
depth
private int depth
-
-
Constructor Details
-
ComparisonDateTool
public ComparisonDateTool()
-
-
Method Details
-
configure
Calls the superclass implementation, then looks for a bundle name and any time units to be skipped. -
getText
Retrieves the specified text resource. -
toYears
public static long toYears(long ms) Returns the number of whole Years in the specified number of milliseconds. -
toMonths
public static long toMonths(long ms) Returns the number of whole Months in the specified number of milliseconds. -
toWeeks
public static long toWeeks(long ms) Returns the number of whole Weeks in the specified number of milliseconds. -
toDays
public static long toDays(long ms) Returns the number of whole Days in the specified number of milliseconds. -
toHours
public static long toHours(long ms) Returns the number of whole Hours in the specified number of milliseconds. -
toMinutes
public static long toMinutes(long ms) Returns the number of whole Minutes in the specified number of milliseconds. -
toSeconds
public static long toSeconds(long ms) Returns the number of whole Seconds in the specified number of milliseconds. -
whenIs
Returns aComparisonDateTool.Comparison
between the result ofDateTool.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.- Parameters:
then
- The date in question
-
whenIs
Returns aComparisonDateTool.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.- Parameters:
now
- The date to use as representative of "now"then
- The date in question
-
difference
Returns aComparisonDateTool.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.- Parameters:
now
- The date to use as representative of "now"then
- The secondary date
-
compare
-
toString
- Parameters:
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 resources
-
toString
Converts the specified positive duration of milliseconds into larger units up to the specified number of positive units, beginning with the largest positive unit. e.g.toString(181453, 3, false, null)
will return "3 minutes 1 second 453 milliseconds",toString(181453, 2, false, null)
will return "3 minutes 1 second", andtoString(180000, 2, true, null)
will return "3 min".
-