Search code examples
velocitydate-format

Change the date format in Velocity


I am a newbie in Velocity. I need to change a certain date format to another.

Eg: Change "Wed May 11 12:49:18 JST 2016" to "2016/05/11 12:49:18"

I found there is a DateTool to be included in VelocityContext in order to use $date.format("myFormat", myDate), but it doesn't work. May be I'm missing something.

I found a similar question, but I think its not answered properly. https://stackoverflow.com/questions/35156429/change-date-format-in-velocity

Thanks in advance!


Solution

  • I added the following code in VelocityContext subclass:

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

    and used this date object in my .vm file like this:

    Order Date : $ctx.date.format('yyyy/MM/dd H:m:s', ${ctx.order.date})

    I referred below link:

    http://www.java2s.com/Code/Java/Velocity/HowtouseDateinVelocity.htm