You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by john mahan <jo...@isg.com> on 2004/11/02 19:50:52 UTC

Velocity Date Calculations?

Using Velocity Template I have tried a variety of DateTool (and MathTool) coding approaches without success (getting NULL result).

The objective is to calculate a Date object comprised of current date plus a variable number of days that is converted to a string for display;

Here is an example of one approach I tried:


 ## -------- Set Min Date
     
     #set ($minDays = 2)  ## Earliest Day valid from Today (0= Today)
     ## ------  Set Initial Calendar Date For Display 
     #set ($minDaysMS = ($minDays * 1000 * 60 * 60 * 24))
     #set ($currDate = $date) ## Default Current System Date
     #set ($baseDate = $date.format('S',$currDate))
     #set ($minDateMS = ($baseDate + $minDaysMS))
     #set ($minDateDisp = $date.toDate($minDateMS))
     #set ($dateDisp = $date.format('M/d/yyyy',$minDateDisp))  ## Min Date Short String 

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I know this is (should be a simple thing) BUT since I'm new to Velocity any suggestions are welcome.

John Mahan

Re: Velocity Date Calculations?

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi John,

> Using Velocity Template I have tried a variety of DateTool (and MathTool) coding approaches without success (getting NULL result).
> 
> The objective is to calculate a Date object comprised of current date plus a variable number of days that is converted to a string for display;
> 
> Here is an example of one approach I tried:
> 
> ## -------- Set Min Date

## snip

> I know this is (should be a simple thing) BUT since I'm new to Velocity any suggestions are welcome.

My suggestion : don't do it in the template.  I doubt this is view logic.
- Have a pre-calculated Date set in the Context, and only format it in
the template
- Make a tool that does the calculation and call the tool in the template.

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Velocity Date Calculations?

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi John,

> but I understand there
> is much to
> do and other priorities exist. In the interim I'll be pleased to use what
> exists and be happy
> to use new things in the future.
You can always submit a patch.  B)

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Velocity Date Calculations?

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi John,

> I believe it will be useful if you can extend the capability  in the future
> to allow the
> Velocity Templates to haveDate manipulation and presentation capability
> similar  to
> JavaScript to avoid needing to use outside methods...
I don't know what you can do in JavaScript,
but maybe what you want is a CalendarTool,
with #roll() and #add() support?
## And, of course, all the other goodies of DateTool.

## I don't like manipulating Date objects directly.  :)

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Velocity Date Calculations?

Posted by john mahan <jo...@isg.com>.
Nathan,

I have been able to use the DateTool in my context but not to calculate the
offset date
for display in  my example (which per your email is because that is not in
the DateTool).
I believe it will be useful if you can extend the capability  in the future
to allow the
Velocity Templates to haveDate manipulation and presentation capability
similar  to
JavaScript to avoid needing to use outside methods... but I understand there
is much to
do and other priorities exist. In the interim I'll be pleased to use what
exists and be happy
to use new things in the future.

THX.,
John Mahan
----- Original Message ----- 
From: "Nathan Bubna" <na...@esha.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Friday, November 05, 2004 11:30 AM
Subject: Re: Velocity Date Calculations?


> john mahan said:
> > Using Velocity Template I have tried a variety of DateTool
> > (and MathTool) coding approaches without success (getting NULL result).
>
> for everything you try?  are you sure you put them in the context?  what
is
> rendered when you do:
>
> $math.class or $date.class ?
>
> if that doesn't render the class names, then you haven't made those tools
> available to your context.  you can do so either by adding them directly:
>
> context.put("date", new DateTool())
>
> or if you are using the VelocityViewServlet, you can register them in your
> toolbox.xml:
>
> <tool>
>     <key>date</key>
>     <scope>application</scope>
>     <class>org.apache.velocity.tools.generic.DateTool</class>
> </tool>
>
> if you don't know what the above examples mean, you need to be reading the
> Velocity and/or VelocityTools documentation and perhaps checking out the
> examples as well.
>
> > The objective is to calculate a Date object comprised of current
> > date plus a variable number of days that is converted to a string for
> display;
> >
> > Here is an example of one approach I tried:
> >
> >
> >  ## -------- Set Min Date
> >
> >      #set ($minDays = 2)  ## Earliest Day valid from Today (0= Today)
> >      ## ------  Set Initial Calendar Date For Display
> >      #set ($minDaysMS = ($minDays * 1000 * 60 * 60 * 24))
> >      #set ($currDate = $date) ## Default Current System Date
> >      #set ($baseDate = $date.format('S',$currDate))
> >      #set ($minDateMS = ($baseDate + $minDaysMS))
>
> this won't work.  you can't add a string ($baseDate) to an integer
> ($minDaysMS) in VTL.
>
> >      #set ($minDateDisp = $date.toDate($minDateMS))
>
> actually, this step won't work either.  the toDate() methods don't
currently
> take integers, only Long instances.  i'm gonna change this now that i
think
> about it, it shouldn't hurt anything to accept integers as well.
>
> >      #set ($dateDisp = $date.format('M/d/yyyy',$minDateDisp))  ## Min
Date
> Short String
>
>
> hope this helps.
>
> Nathan Bubna
> nathan@esha.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Velocity Date Calculations?

Posted by Nathan Bubna <na...@esha.com>.
john mahan said:
> Using Velocity Template I have tried a variety of DateTool
> (and MathTool) coding approaches without success (getting NULL result).

for everything you try?  are you sure you put them in the context?  what is
rendered when you do:

$math.class or $date.class ?

if that doesn't render the class names, then you haven't made those tools
available to your context.  you can do so either by adding them directly:

context.put("date", new DateTool())

or if you are using the VelocityViewServlet, you can register them in your
toolbox.xml:

<tool>
    <key>date</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.DateTool</class>
</tool>

if you don't know what the above examples mean, you need to be reading the
Velocity and/or VelocityTools documentation and perhaps checking out the
examples as well.

> The objective is to calculate a Date object comprised of current
> date plus a variable number of days that is converted to a string for
display;
>
> Here is an example of one approach I tried:
>
>
>  ## -------- Set Min Date
>
>      #set ($minDays = 2)  ## Earliest Day valid from Today (0= Today)
>      ## ------  Set Initial Calendar Date For Display
>      #set ($minDaysMS = ($minDays * 1000 * 60 * 60 * 24))
>      #set ($currDate = $date) ## Default Current System Date
>      #set ($baseDate = $date.format('S',$currDate))
>      #set ($minDateMS = ($baseDate + $minDaysMS))

this won't work.  you can't add a string ($baseDate) to an integer
($minDaysMS) in VTL.

>      #set ($minDateDisp = $date.toDate($minDateMS))

actually, this step won't work either.  the toDate() methods don't currently
take integers, only Long instances.  i'm gonna change this now that i think
about it, it shouldn't hurt anything to accept integers as well.

>      #set ($dateDisp = $date.format('M/d/yyyy',$minDateDisp))  ## Min Date
Short String


hope this helps.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org