Wednesday, November 27, 2013

Date formatting in Velocity templates

Here's how to format a date inside a velocity template. Add an additional velocity-tools library in your dependencies:

     org.apache.velocity
     velocity-tools
     2.0
 
Import the DateTool class:
import org.apache.velocity.tools.generic.DateTool;
Add an instance of this class to the VelocityContext:
VelocityContext context = new VelocityContext();
context.put("date", new DateTool());
Add your date object to the context:
context.put("some_date", new Date());
Use the DateTool parameter in the template to format date:
$date.format('dd.MM.yyyy', $some_date)

No comments:

Post a Comment