Search code examples
javavelocity

How to select the format of $date in vm file?


I have a $date defined as "day of week, month day, year" ex: Tuesday, February 26, 2013

I don't know where $date is defined but I like to add the hour to this $date variable, or create a variable with the hour, do you know how can I put it in the .vm file?


Solution

  • Velocity provides a DateTool class for formatting dates. You would need to put an instance of this class into your velocity context:

    context.add("date", new DateTool());
    

    Then you could use a formatting command like:

    $date.format('EEE, MMM d, yyyy at ha', $myDate)
    

    to get something like Tuesday, February 26, 2013 at 11AM