On 09/29/2011 04:21 PM, Scott Serr wrote:
----- "Scott Serr"<serrs(a)theserrs.net> wrote:
> In the dev mailing list, I saw years ago that $datetool was changed to
> point to ComparisonDateTool. This gave it added functionality. Also,
> the documentation on
>
http://platform.xwiki.org/xwiki/bin/view/DevGuide/Scripting says it is
> a ComparisonDateTool.
>
> My problem is, it doesn't act like a ComparisonDateTool, it acts like
> DateTool.
To check the class of an object, just call:
$datetool.class
For me, it prints the expected classname:
class org.apache.velocity.tools.generic.ComparisonDateTool
Works:
$datetool.get('dd.MM.yyyy HH:mm')
Does not work:
$datetool.difference('2005-07-04','2007-02-15')
Apparently I can't pass String as Object, I have to pass Date?
Reading the Javadoc of DateTool, it states:
"It is useful for accessing and formatting the "current" date as well as
for formatting arbitrary Date and Calendar objects."
So it looks like indeed it can only be used with real dates, not
strings, although the javadoc of ComparisonDateTool suggests otherwise.
So, I looked at the code, since that is the definitive documentation,
and it is supposed to work with strings. Looking deeper, it seems that
the problem is that Strings are converted to dates using a standard date
format, so you can only parse dates using the DEFAULT format defined in
java.text.DateFormat. So, while this doesn't work:
$datetool.toDate('2005-07-04')
This works:
$datetool.toDate('Jul 4, 2005 09:00:00 AM')
It also works if you specify the date format to use:
$datetool.toDate('yyyy-MM-hh', '2005-07-04')
So, to solve your problem, you should first parse the dates, then
compare them.
Anyway, sorry for the traffic.
--
Sergiu Dumitriu
http://purl.org/net/sergiu/