DateTool - Documentation topics on: calendar,date format,date formatting,dates,datetool,parse dates,velocity date and calendar tools,velocity date tool,velocity tools,.

DateTool

This is a view tool for working with Date and Calendar in Velocity templates. This tool is used to:

  • Retrieving the Current Date
  • Formatting Dates to Strings
  • Parsing String into Dates

Valid date formats

Uses Java's SimpleDateFormat When formatting and parsing dates to and from Strings, you may specify a format using the accepted Symbols below.


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)"

Formatting:

  $date                         -> Oct 19, 2003 9:54:50 PM (current date/time)
  $date.long                    -> October 19, 2003 9:54:50 PM PDT (current date/time long format)
  $date.medium_time             -> 9:54:50 PM
  $date.full_date               -> Sunday, October 19, 2003
  $date.get('default','short')  -> Oct 19, 2003 9:54 PM
  $date.get('yyyy-M-d H:m:s')   -> 2003-10-19 21:54:50
  $date.format('medium',$myDate)-> Oct 7, 2003 3:14:50 AM

Parsing:

 $date.toDate('yyyy-M-d H:m:s',"2003-10-19 21:54:50")-> Date Object
 $date.toDate( "yyyy-MM-dd'T'HH:mm:ss'Z'","2016-05-06T20:42:26Z"))->  Date Object

Adding/subtracting dates

This code gets a calendar object and adds 24 hours to it:

My date: $date

## Get a java Calendar
#set($x = $date.calendar)

## Add 24 hours (int code 10)
$x.add(10,24)

## Show result
My new date :$x.time

java Calendar Constants

Here are the calendar constants - these are used with a java calendar in order to add/subtract and manipulate a date by a particular dimension:

java.util.Calendar
AM 0
AM_PM 9
APRIL 3
AUGUST 7
DATE 5
DAY_OF_MONTH 5
DAY_OF_WEEK 7
DAY_OF_WEEK_IN_MONTH 8
DAY_OF_YEAR 6
DECEMBER 11
DST_OFFSET 16
ERA 0
FEBRUARY 1
FIELD_COUNT 17
FRIDAY 6
HOUR 10
HOUR_OF_DAY 11
JANUARY 0
JULY 6
JUNE 5
LONG 2
MARCH 2
MAY 4
MILLISECOND 14
MINUTE 12
MONDAY 2
MONTH 2
NOVEMBER 10
OCTOBER 9
PM 1
SATURDAY 7
SECOND 13
SEPTEMBER 8
SHORT 1
SUNDAY 1
THURSDAY 5
TUESDAY 3
UNDECIMBER 12
WEDNESDAY 4
WEEK_OF_MONTH 4
WEEK_OF_YEAR 3
YEAR 1
ZONE_OFFSET 15