16

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?

aF.
  • 58,798
  • 40
  • 127
  • 191

4 Answers4

29

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

Stephen Ostermiller
  • 18,578
  • 8
  • 74
  • 95
  • I can put that line on .vm file and I'll get the hour formated like I want? – aF. Feb 26 '13 at 17:18
  • Unfortunately I've put that and it just shows "EEE, MMM d, yyyy at ha" instead of the values itselft, do you know what might be? – aF. Feb 26 '13 at 17:29
  • 7
    If that is a case then a DateTool is not in your VelocityContext. Edit your velocity context like this in your java files: `context.add("date", new DateTool());` – Stephen Ostermiller Feb 26 '13 at 17:31
8

Alternative solution that doesn't require additional dependency or code modification:

#set( $String = '' )##
$String.format('%1$tY%1$tm%1$td%1$tH%1$tM%1$tS', $date)

Combined from two other answers.

Community
  • 1
  • 1
Vadzim
  • 21,258
  • 10
  • 119
  • 142
  • Don't know what have changed but this trick does not work with maven archetype plugin that uses Velocity. I can call non-static methods on String instance, but not `format`. – Aleksandr Kravets Apr 25 '17 at 11:03
  • 1
    @AleksandrKravets May be the answer by Gus here: https://stackoverflow.com/questions/2329191/calling-class-methods-static-from-inside-a-velocity-view-page/15126547#15126547 explains this. – hcg Feb 28 '18 at 14:05
4

From the documentation:

Symbol   Meaning                 Presentation        Example
   ------   -------                 ------------        -------
   G        era designator          (Text)              AD
   y        year                    (Number)            1996
   M        month in year           (Text & Number)     July & 07
   d        day in month            (Number)            10
   h        hour in am/pm (1~12)    (Number)            12
   H        hour in day (0~23)      (Number)            0
   m        minute in hour          (Number)            30
   s        second in minute        (Number)            55
   S        millisecond             (Number)            978
   E        day in week             (Text)              Tuesday
   D        day in year             (Number)            189
   F        day of week in month    (Number)            2 (2nd Wed in July)
   w        week in year            (Number)            27
   W        week in month           (Number)            2
   a        am/pm marker            (Text)              PM
   k        hour in day (1~24)      (Number)            24
   K        hour in am/pm (0~11)    (Number)            0
   z        time zone               (Text)              Pacific Standard Time
   '        escape for text         (Delimiter)
   ''       single quote            (Literal)           '

   Examples: "E, MMMM d" will result in "Tue, July 24"
             "EEE, M-d (H:m)" will result in "Tuesday, 7-24 (14:12)"

Hope that helps

Matt Sich
  • 3,195
  • 1
  • 19
  • 26
2

One of the backing Java classes must be putting it into the Context. If you want to format the date differently, you can do it in that class.

Another option would be to put the raw Date object into the context, then call methods in the Velocity template to format it. If need be you can pass Apache Commons DateUtils or another helper class to the template as well (see this answer).

Community
  • 1
  • 1
Evan Haas
  • 2,456
  • 2
  • 20
  • 33
  • Can't I put something on the .vm template file that will do what I need? – aF. Feb 26 '13 at 17:30
  • What is the type of `$date`? If unsure, put `$date.getClass()` in your .vm. If it's a String, then no, you'll have to do the formatting on the Java side. If it's a Date, then yes, you can call methods in the .vm to format it. – Evan Haas Feb 26 '13 at 18:16