The Date Velocity Viewtool provides methods to manipulate and display dates and times in Velocity.
The following example shows how the Date Viewtool is mapped in the toolbox.xml file:
<tool>
<key>date</key>
<scope>request</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
</tool>
The DateTool makes it easy to:
- Retrieve the current date
- Format date objects as Strings
- Parse Strings into date objects
Date Formats
The DateTool uses the Java SimpleDateFormat. When converting between date objects and Strings, you may specify the format using following symbols in the format string:
Symbol | Description | Value Type | Examples |
---|---|---|---|
G | Era | Text | AD , BC |
y | Year (full) | Number | 1996 , 2017 |
yy | Year (last 2 digits) | Number | 96 , 17 |
M | Month | String and Number July & 07 | |
MMM | Month name (abbrev.) | Text | Jul , Dec |
MMMM | Month name (full name) | Text | July , December |
d | Day of month | Number | 3 , 27 |
dd | Day of month (zero-padded) | Number | 03 , 27 |
h | hour in am/pm (1~12) | Number) 12 | |
H | hour in day (0~23) | Number) 0 | |
m | Minute | Number | 7 , 49 |
mm | Minute (zero-padded) | Number | 07 , 49 |
s | Second | Number | 1 , 55 |
ss | Second (zero-padded) | Number | 01 , 55 |
S | Millisecond | Number | 978 |
E | Day of week | String | Wednesday |
EEE | Day of week (abbrev.) | String | Wed |
D | Day of year | Number | 189 |
F | Occurrence of Day in month | Number | 2 (e.g. 2nd Wednesday in July) |
w | Week of year | Number | 27 |
W | Week of month | Number | 2 |
a | AM/PM | String | PM |
k | Hour (24 hour time) | Number) 24 | |
K | Hour (as AM/PM) | Number) 0 | |
z | Time zone | Text | Pacific Standard Time |
zzz | Time zone (abbrev.) | Text | PST |
' | Literal text delimiter | Text | 'Date: ' |
'' | Embedded single quote | Literal | '' |
Examples
Format String | Example Date (for Tue, July 3, 2016) |
---|---|
“E, MMMM d” | “Tue, July 3” |
“EEE, M-d (H:m)” | “Tuesday, 7-3 (14:12)” |
“'ABC' E '' MMM dd” | “ABC Tue ' Jul 03” |
Properties and Methods
The following are some commonly used DateTool properties and methods:
Value | Return Value | Example Result |
---|---|---|
$date | Current date & time, in default format | Oct 19, 2003 9:54:50 PM |
$date.long | Current date & time, in “long” format | October 19, 2003 9:54:50 PM PDT |
$date.medium_time | Current time, in “medium_time” format | 9:54:50 PM |
$date.full_date | Current date, in “full_date” format | Sunday, October 19, 2003 |
$date.get('default','short') | Current date & time, in “short” format | Oct 19, 2003 9:54 PM |
$date.get('yy-M-d H:m:s') | Current date & time, in the specified format | 03-10-19 21:54:50` |
$date.format('medium',$myDate) | Format the date in the $myDate object using “medium” date format | Oct 7, 2003 3:14:50 AM |
$date.getDate() | A Date object for the current date & time | |
$date.getYear() | The current year | 2003 |
$date.getMonth() | The current month | 10 |
$date.getDay() | The current day of the month | 19 |
In addition, the DateTool supports many additional properties and methods; for a detailed list of DateTool methods, please see the DateTool Javadocs.
Parsing Date Strings
You can extract date objects from date strings using the toDate
method, and supplying the appropriate format string, as in the following examples:
#set( $dateObj = $date.toDate( "yyyy-M-d H:m:s", "2003-10-19 21:54:50") )
#set( $dateObj = $date.toDate( "yyyy-MM-dd'T'HH:mm:ss'Z'", "2016-05-06T20:42:26Z") )
Adding and Subtracting Dates
Example {AddSubtractExample}
The following example code retrieves a calendar object and adds 24 hours to the time of the object:
<p>Original Date: $date</p>
## Get a Java Calendar Object
#set( $x = $date.calendar )
## Add 24 hours (int code 10)
$x.add( 10, 24 )
## Display the result
<p>New Date :${esc.d}x.time</p>
Multilingual Dates and Times
By default, the date tool will display all date and time related names using the default language, regardless of the language of the page or content where the DateTool is being called from. However you may set the locale used by the DateTool using Velocity, and then specify the locale to use when calling the DateTool methods.
The following example code demonstrates how to call DateTool methods using the language currently set for the page or content where the DateTool is being used:
## Get and display the month and day of week using the default language:
#set( $defaultLocale = $date.getLocale() )
<p>Default Locale (${defaultLocale.language}-${defaultLocale.country}): $date.format("MMMM E", $date.getDate(), $defaultLocale)</p>
## Get the user's currently selected dotCMS language and construct the code for it:
#set( $language = $globalvars.getLanguage( $request.getSession().getAttribute('com.dotmarketing.htmlpage.language') ) )
#set( $languageCode = "$language.getLanguageCode()" )
#set( $countryCode = "$language.getCountryCode()" )
#set( $finalLangCode = "${languageCode}-${countryCode}" )
## Get the locale and display the month and day of week using the user's currently selected language:
#set( $locale = $defaultLocale.forLanguageTag( $languageCode ) )
<p>Current Language (${locale.language}-${locale.country}): $date.format("MMMM E", $date.getDate(), $locale)</p>
## Display time in UTC, regardless of locale:
#set( $defaultLocale = $date.getLocale() )
#set( $outTimeZone = $date.getTimeZone().getTimeZone('UTC') )
#set( $myDate = $date.getDate() )
<p>UTC time: ${date.format("yyyy-M-d H:m:s", $myDate, $defaultLocale, $outTimeZone)}</p>
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 |