Hi,
I have start Date and End date so how I can check holidays fall in between these two dates?
How many holidays fall ?
I have script runner plugin. How can I write script to find this?
Any help much appreciated.
Hi @Omprakash Thamsetty
You can use scriptrunner to generate a script field to hold the value or use as a script check in
Script Console, however you gonna need mantain an array with the holidays of the year, i know it's possible connect with some services to get the holidays of the year, but some of them are paid service, so you can also explore the connection with public API's to get the information and keep them in the array.
Here is the manual version of the script i mencioned:
import java.time.LocalDateimport java.time.format.DateTimeFormatter// Input dates (you can replace these with your issue's field values or parameters)def startDate = LocalDate.parse("2025-01-01", DateTimeFormatter.ISO_DATE)def endDate = LocalDate.parse("2025-01-10", DateTimeFormatter.ISO_DATE)// Define the list of holidays (in YYYY-MM-DD format)def holidays = [LocalDate.parse("2025-01-06"),LocalDate.parse("2025-01-09")]// Filter holidays that fall within the rangedef holidaysInRange = holidays.findAll { it.isAfter(startDate.minusDays(1)) && it.isBefore(endDate.plusDays(1)) }// Output the resultdef holidayCount = holidaysInRange.size()log.info("Number of holidays between ${startDate} and ${endDate}: ${holidayCount}")// Return the final valuereturn holidayCount
Let me know it this helps you.
Best Regards
I've given a similar script using ScriptRunner's Behaviour in this Community Question.
Thank you and Kind regards,Ram
Hi @Kawan Diogo
I tried getting the date field values from jira issue as its get as string so converting to Date format. Look like my script isn't working. Could you please see what is missing.
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.event.type.EventDispatchOptionimport com.atlassian.jira.issue.MutableIssueimport org.apache.log4j.Categoryimport com.atlassian.jira.issue.IssueManager;import java.sql.Timestampimport com.atlassian.jira.issue.ModifiedValueimport com.atlassian.jira.issue.util.DefaultIssueChangeHolderimport com.atlassian.jira.issue.util.IssueChangeHolderimport com.atlassian.jira.issue.*import java.util.Dateimport java.text.SimpleDateFormatimport java.text.DateFormatimport java.text.ParseExceptionimport java.sql.Timestampimport com.atlassian.mail.Emailimport com.atlassian.mail.server.MailServerManager;import com.atlassian.mail.server.SMTPMailServer;import com.atlassian.mail.queue.MailQueueimport com.atlassian.mail.queue.SingleMailQueueItemimport java.time.LocalDateimport java.time.format.DateTimeFormatter import java.text.DateFormat//////////////////////////////////////////////////////////////////////////////////////////Used for testing on specific issuedef IssueKey = "GT-5429"def issue = ComponentAccessor.getIssueManager().getIssueObject(IssueKey)////////////////////////////////////////////////////////////////////////////////////////Calendar cal = Calendar.getInstance();// Logger//def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()def IssueManager issueManager = ComponentAccessor.issueManagerIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();//MutableIssue issue = event.issue as MutableIssue // Set the logging level to DEBUGlog.setLevel(org.apache.log4j.Level.DEBUG);def cf_pirResultDate = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("PIR Results/LL Due Date").first()def cf_impdt = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Implementation Date").first()def cf_datedt = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Date").first()// Define the list of holidays (in YYYY-MM-DD format)def holidays = [LocalDate.parse("2025-01-06"),LocalDate.parse("2025-01-09")]String cf_impdtValue = issue.getCustomFieldValue(cf_impdt).toString()String cf_datedtValue = issue.getCustomFieldValue(cf_datedt).toString()def datePattern = "yyyy-MM-dd";DateFormat formatter = new SimpleDateFormat(datePattern);Date Startdate = formatter.parse(cf_impdtValue);Date EndDate = formatter.parse(cf_datedtValue);def holidaysInRange = holidays.findAll { it.isAfter(Startdate.minusDays(1)) && it.isBefore(EndDate.plusDays(1)) }
You need to convert the Date to LocalDate to make the correct comparison with the holidays. Additionally, I recommend adding some logs to ensure every field is set with a value.
Here’s a new version with these adjustments. Please try it and let me know if it works.
import com.atlassian.jira.component.ComponentAccessorimport org.apache.log4j.Categoryimport java.text.SimpleDateFormatimport java.util.Dateimport java.time.LocalDate// Loggerdef log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");log.setLevel(org.apache.log4j.Level.DEBUG);// Issue Key for testingdef IssueKey = "GT-5429"def issue = ComponentAccessor.getIssueManager().getIssueObject(IssueKey)if (!issue) {log.error("Issue not found.");return;}// Custom fieldsdef cf_impdt = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Implementation Date")?.first()def cf_datedt = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Date")?.first()if (!cf_impdt || !cf_datedt) {log.error("Required custom fields not found.");return;}// Custom field valuesString cf_impdtValue = issue.getCustomFieldValue(cf_impdt)?.toString()String cf_datedtValue = issue.getCustomFieldValue(cf_datedt)?.toString()if (!cf_impdtValue || !cf_datedtValue) {log.error("Custom field values are null or empty.");return;}// Parse datesdef datePattern = "yyyy-MM-dd";Date Startdate, EndDate;try {def formatter = new SimpleDateFormat(datePattern);Startdate = formatter.parse(cf_impdtValue);EndDate = formatter.parse(cf_datedtValue);} catch (Exception e) {log.error("Error parsing dates: ${e.message}");return;}// Convert Date to LocalDatedef startDate = Startdate.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();def endDate = EndDate.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();// Define holidaysdef holidays = [LocalDate.parse("2025-01-06"),LocalDate.parse("2025-01-09")]// Find holidays in rangedef holidaysInRange = holidays.findAll { it.isAfter(startDate.minusDays(1)) && it.isBefore(endDate.plusDays(1)) }log.debug("Holidays in range: ${holidaysInRange}");
Hi @Kawan Diogo Yes, This worked.
I tried converting string to LocalDate but couldn't not success but your code works fine.
Looks tome I am good with this now. Thank you so much.
It looks like you're new here. Sign in or register to get started.