Calculate the time issue have been created without holidys

Harry Huang September 9, 2019

Hi gus,

 

I want a field to calculate the time issue have been created without holidys (saturday&sunday).

But I don't kown how to do it.

 

Below is my code, but it is include holidays.

How can I  except holidays?

 

def haveBeenCreatedTime = System.currentTimeMillis() - issue.created.getTime()
return (haveBeenCreatedTime / 1000 / 3600 / 24 ) as long 

 

1 answer

1 accepted

0 votes
Answer accepted
Mohamed Adel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 9, 2019

@Harry Huang 

 

You need to use a another method to calculate the the non-working days between your start/end date then deduct the return value from "haveBeenCreatedTime"

 

in your case startcalTime = System.currentTimeMillis() , endCal = issue.created.getTime()

 


def countNonWorkingDays(def startcalTime, def endCal) {

Calendar startCal = new GregorianCalendar()
startCal.setTimeInMillis(startcalTime)
def iHolydayCnt = 0
while (startCal.getTimeInMillis() < endCal) {
if ((startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
|| (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
) {
++iHolydayCnt
}
startCal.add(Calendar.DAY_OF_YEAR, 1)
}
return iHolydayCnt
}
Harry Huang September 9, 2019

@Mohamed Adel Thank you for your answer, but this line will be error

2019-09-10_141942.png

Harry Huang September 9, 2019
def startcalTime = System.currentTimeMillis()
def endCal = issue.created.getTime()

def countNonWorkingDays(def startcalTime, def endCal) {

Calendar startCal = new GregorianCalendar()
startCal.setTimeInMillis(startcalTime)
def iHolydayCnt = 0
while (startCal.getTimeInMillis() < endCal) {
if ((startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
{
++iHolydayCnt
}
startCal.add(Calendar.DAY_OF_YEAR, 1)
}
return iHolydayCnt
}

 

Mohamed Adel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 9, 2019


@Harry Huang 

please ignore the console error and use the following code as you need to call the method to get the value . 

 

def startcalTime = System.currentTimeMillis()
def endCal = issue.created.getTime()

def nonWorkingDays = countNonWorkingDays(startcalTime,endCal)

def haveBeenCreatedTime = System.currentTimeMillis() - issue.created.getTime() - nonWorkingDays*(60*60*24*1000)


def countNonWorkingDays(def startcalTime, def endCal) {

Calendar startCal = new GregorianCalendar()
startCal.setTimeInMillis(startcalTime)
def iHolydayCnt = 0
while (startCal.getTimeInMillis() < endCal) {
if ((startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
{
++iHolydayCnt
}
startCal.add(Calendar.DAY_OF_YEAR, 1)
}
return iHolydayCnt
}
Harry Huang September 10, 2019

@Mohamed Adel Thank you for your answer again. But it seems still contain holidays

2019-09-10_150133.png

Mohamed Adel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 10, 2019

@Harry Huang 

Sorry for the confusion, in your case 

endCal = system.currentTimeMillis()

startCal = issue.created.getTime()

regards,

Harry Huang September 15, 2019

Finally, this code is totally achieve my requirement.

@Mohamed Adel Thanks a lot.

 

Calendar c1 = new GregorianCalendar()
c1.setTimeInMillis(issue.created.getTime())

int iHolydayCnt = 0

while(c1.getTimeInMillis() < System.currentTimeMillis() ){ 
c1.add(Calendar.DATE, 1)
if((c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)){
++iHolydayCnt
}
}

def haveBeenCreatedTime = System.currentTimeMillis() - issue.created.getTime() - iHolydayCnt*(60*60*24*1000)

return (haveBeenCreatedTime / 1000 / 3600 / 24 ) as long

Suggest an answer

Log in or Sign up to answer