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>
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>