Example scripts
To homepage
Jira

Set the Value of Date or Date-Time Fields
App in script

ScriptRunner For Jira
by Adaptavist
Compatibility

Jira (8.0 - 8.14)

ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.datetime.DateTimeFormatter as JiraDateTimeFormatter
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.TemporalAccessor
import java.time.format.DateTimeFormatter
@BaseScript FieldBehaviours fieldBehaviours
// Set log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)
// The date & date time field names to set a value
final dateFieldName = "DateFieldA"
final dateTimeFieldName = "DateTimeFieldA"
// Get the components
def customFieldManager = ComponentAccessor.customFieldManager
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
// Get the Jira date time formatter for the logged in user, so we can get the correct locale and user timezone
def userFormatter = ComponentAccessor.getComponent(JiraDateTimeFormatter).forUser(authenticationContext.loggedInUser)
// Get the date & date time field objects
def dateField = customFieldManager.getCustomFieldObjectsByName(dateFieldName).first()
def dateTimeField = customFieldManager.getCustomFieldObjectsByName(dateTimeFieldName).first()
// Set the wanted dates params for values, format pattern and timezone to interpret the dates in
final timeZoneId = ZoneId.systemDefault()
final dateFormatter = DateTimeFormatter.ofPattern('yyyy/MM/dd')
final dateTimeFormatter = DateTimeFormatter.ofPattern('yyyy/MM/dd HH:mm:ss')
final wantedDateAsText = '2020/11/28'
final wantedDateTimeAsText = '2020/11/01 12:12:00'
// Parse the dates as interpreted in the chosen time zone and convert to the user time zone, to make sure the user sees the date in his time zone
def userTimeZoneId = userFormatter.zone.toZoneId()
def wantedDate = LocalDate.parse(wantedDateAsText, dateFormatter)
.atStartOfDay(timeZoneId)
.withZoneSameInstant(userTimeZoneId)
def wantedDateTime = LocalDateTime.parse(wantedDateTimeAsText, dateTimeFormatter)
.atZone(timeZoneId)
.withZoneSameInstant(userTimeZoneId)
// Populate the fields
getFieldByName("DateFieldA").setFormValue(getJiraDateFormattedForUser(wantedDate, dateField, userFormatter))
getFieldByName("DateTimeFieldA").setFormValue(getJiraDateFormattedForUser(wantedDateTime, dateTimeField, userFormatter))
/**
* Get the date or date time values in the format expected by Jira an behaviours for fields
* @param inputDate The input date or date time to format
* @param sourceField The field object in which the value will be set
* @param dateTimeFormatter the Jira's date time formatter to extract the user's locale info
* @return The formatted date time
*/
static String getJiraDateFormattedForUser(TemporalAccessor inputDate, CustomField sourceField, JiraDateTimeFormatter dateTimeFormatter) {
// Get the Jira date format property key depending on the field type (date or date time)
def dateFormatPatternProperty = (sourceField.customFieldType in DateTimeCFType) ? APKeys.JIRA_DATE_TIME_PICKER_JAVA_FORMAT : APKeys.JIRA_DATE_PICKER_JAVA_FORMAT
// Get the format string, so we can format dates how Jira expects
def dateFormatPattern = ComponentAccessor.applicationProperties.getDefaultBackedString(dateFormatPatternProperty)
// Build the formatter object
def formatterForFieldType = DateTimeFormatter.ofPattern(dateFormatPattern, dateTimeFormatter.locale)
// Return the formatted date string that can be used to set date or date time fields
formatterForFieldType.format(inputDate)
}
Having an issue with this script?
Report it here