Sometimes we need to create scripts that checks if the current date is the first monday of the month, or something like this.

See here some code tricks to operate with Date/Calendar classes quickly

import java.text.SimpleDateFormat
import java.sql.Timestamp
import java.text.DateFormat
import java.util.Date

//To get the actual DATE
def today = new Date().format('yyyy-MM-dd');
def date = new Date();
def day = new Date().format('dd');
def year = new Date().format('yyyy');
def month = new Date().format('MM');

//To set a fixed DATE
def fecha = '2016-02-01';
def today = new Date().parse('yyyy-MM-dd', fecha).format('yyyy-MM-dd');
def date = new Date().parse('yyyy-MM-dd', fecha);
def day = new Date().parse('yyyy-MM-dd', fecha).format('dd');
def year = new Date().parse('yyyy-MM-dd', fecha).format('yyyy');
def month = new Date().parse('yyyy-MM-dd', fecha).format('MM');

//To get the WEEKDAY
def weekday = Date.parse('yyyy-MM-dd', today)[Calendar.DAY_OF_WEEK];

Calendar cacheCalendar = Calendar.instance
cacheCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cacheCalendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
cacheCalendar.set(Calendar.MONTH, (Integer.parseInt(month)-1));
cacheCalendar.set(Calendar.YEAR, Integer.parseInt(year));
def fecha_primer_lunes = cacheCalendar.time.format("yyyy-MM-dd")

if ( today == fecha_primer_lunes) {
//We can do estrange things, for example
def datefrom = (date - 7).format('yyyy-MM-01');

//To get the WEEK_OF_THE_YEAR
Calendar cal = Calendar.getInstance(new Locale("es", "ES"));
int week = cal.get(Calendar.WEEK_OF_YEAR);

//Function to calculate the working days between two dates
int calculate_working_days (Date date_start, Date date_end ) {
	def date_limit = date_end.format('yyyy-MM-dd');
	def date_aux = date_start.format('yyyy-MM-dd');
    def date = date_start;
	def i = 0;
    def days = 0;
    def weekday = null;
	while ( date_aux < date_limit ) {
        weekday = Date.parse('yyyy-MM-dd', date_aux)[Calendar.DAY_OF_WEEK];
        if (!( weekday == Calendar.SUNDAY || weekday == Calendar.SATURDAY )) {
    	date = date + 1;
        date_aux = date.format('yyyy-MM-dd');
    return days;

//to get the working days of the previous week if it's sunday
if ( weekday == Calendar.SUNDAY ) {
calculate_working_days ( Date.parse('yyyy-MM-dd', (date - 7).format('yyyy-MM-dd')),  Date.parse('yyyy-MM-dd', (date - 0).format('yyyy-MM-dd')))

//to get the working days of the previous month if it's day 1 of the month
if ( day == '1' || day == '01') {
calculate_working_days ( Date.parse('yyyy-MM-dd', (date - 1).format('yyyy-MM-01')),  Date.parse('yyyy-MM-dd', (date - 1).format('yyyy-MM-dd')))

Posted by:.

2 replies on “Some JIRA Groovy Date & Calendar script tricks

  1. Hello,
    sometimes when you find this post in Internet is because you have troubbles for updating customField type date in Scriptrunner.
    The following code shows you how to do it if you want to set a “hardCoded” Date.

    I hope it helps.

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.ModifiedValue
    import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
    import com.atlassian.jira.issue.Issue
    import com.atlassian.jira.event.type.EventDispatchOption
    import java.sql.Timestamp

    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
    def cfPlannedStart = customFieldManager.getCustomFieldObjectByName(“Planned start”)

    def String startSemDate = “2021-01-01 00:00:00.0”
    Date startSemesterDate = new Date().parse(‘yyyy-MM-dd’, startSemDate)
    cfPlannedStart.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfPlannedStart),new Timestamp(startSemesterDate.time)), new DefaultIssueChangeHolder())

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s