Work with Dates in velocity templates
JEMH velocity context, relating to emails (comment header specifically) has $sentDate
(if present, reverts to now() if not) as well as $receivedDate
. Presentation of these dates can be altered through helper classes in the velocity context.
Lower down there are several date operators that are hyperlinked to API Documentation so you know what you can do with them:
For example:
1a) $dateFormatter.format($sentDate)
1b) $dateFormatter.forLoggedInUser().withStyle($dateTimeStyle.RSS_RFC822_DATE_TIME).format($sentDate)
1c) $dateFormatter.forLoggedInUser().withStyle($dateTimeStyle.DATE_TIME_PICKER).format($sentDate)
1d) $dateFormatter.forLoggedInUser().withStyle($dateTimeStyle.RELATIVE).format($sentDate)
2) $outlookdate.formatDMY($sentDate) $outlookdate.formatTime($sentDate)
3) $dateformatter.getNow() / $dateformatter.formatIso8601($dateformatter.getNow())
4) $dateTimes.formatRSS($sentDate)
Generates:
1a) 16/Jan/15 10:47 AM
1b) Fri, 16 Jan 2015 10:47:48 +0000
1c) 16/Jan/15 10:47 AM
1d) Today 10:47 AM
2) 16/Jan/15 10:47 AM
3) Fri Jan 16 10:47:48 GMT 2015 / 2015-01-16T10-47
4) Fri, 16 Jan 2015 10:47:48 +0000
Velocity Custom Field Dates
JEMH can build date objects too, to enable values to be generated for arbitrary dates, e.g.:
Date Time
#set($longVal = $currentMillis.longValue() + 86400000 )
$dateFormatter.format($jemhDateUtils.createDate($longVal))
Example Output:
29/Mar/15 3:41 PM
Date Only
#set($longVal = $currentMillis.longValue() + 86400000 )
$dateFormatter.formatDMY($jemhDateUtils.createDate($longVal))
Example Output:
29/Mar/15
DatePicker custom fields
$dateFormatter.formatDatePicker($issue.getCustomFieldValue("customfield_10700"))
For further Date formatting options please see:
JEMHDateUtils
since 3.2.6
JEMHDateUtils is another interface written by JEMH to accommodate certain functionality which is not achievable when working with Dates obtained from the email.
#set ($headerDate = "Wed, 19 Feb 2020 14:50:59 +0000")
#set ($dateFormat = "E, d MMM yyyy HH:mm:ss Z")
#set ($date = $jemhDateUtils.getZonedDateTime($headerDate, $dateFormat))
The date/time in the email is: $date.format($jemhDateUtils.getFormatter($dateFormat))
#set ($zone = $jemhDateUtils.getZoneId("America/Los_Angeles"))
#set ($laTime = $date.withZoneSameInstant($zone))
In Los Angeles this is: $laTime.format($jemhDateUtils.getFormatter($dateFormat))
Debugging velocity rendered custom fields as above is a bit tedious (issue JEMH-7794 logged to pick and evaluate script against a stored JEMH Test Case email). For rapid testing against a Test Case, you can do the same thing in a Script Field Processor edit window:
if (message.getHeader("Date") !=null)
{
var headerDate = message.getHeader("Date")[0];
var dateFormat = "E, d MMM yyyy HH:mm:ss Z";
var date = jemhDateUtils.getZonedDateTime(headerDate, dateFormat);
print ('The date/time in the email is: ' +date.format(jemhDateUtils.getFormatter(dateFormat)) );
var zone = jemhDateUtils.getZoneId("America/Los_Angeles");
var laTime = date.withZoneSameInstant(zone);
print ('In Los Angeles this is: '+laTime.format(jemhDateUtils.getFormatter(dateFormat)) );
}
else
{
print ('no Date: header');
}
The key functionality to note:
ZonedDateTime - we are using ZonedDateTime which allows us to retain the time zone aspect along with the date + time. This is vital aspect in case you’d wish to convert this to another time zone (which is what is being done in the above example)
Formatter - to format the date in a certain pattern (i.e. 12/02/2020 - 08:29), we have provided the
getFormatter(pattern)
method which is responsible for returning a DateTimeFormatter. The same can be used to format the ZonedDateTime to any other preferred format
Output:
The date/time in the email is: Wed, 19 Feb 2020 14:50:59 +0000
In Los Angeles this is: Wed, 19 Feb 2020 06:50:59 -0800
Creating relative dates in custom fields
@since 3.3.27
This set of methods doesn’t depend on any Jira user in the authentication context, e.g., you want to do date/time manipulation for a custom field default
See formatter parameters here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
$jemhDateUtils.createLocalDate().plusDays(15).format($jemhDateUtils.getFormatter("dd/MMM/yy"))
If you want to use the Jira format:
#set ($javaDateFormat = $applicationProperties.getDefaultBackedString("jira.date.picker.java.format"))
#set ($formatter = $jemhDateUtils.getFormatter($javaDateFormat))
$jemhDateUtils.createLocalDate().plusDays(14).format($formatter)