Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,466,623
Community Members
 
Community Events
176
Community Groups

Calculate the time issue have been created without holidys

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 Community Leader Sep 09, 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
}

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

2019-09-10_141942.png

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 Community Leader Sep 09, 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
}

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

2019-09-10_150133.png

Mohamed Adel Community Leader Sep 10, 2019

@Harry Huang 

Sorry for the confusion, in your case 

endCal = system.currentTimeMillis()

startCal = issue.created.getTime()

regards,

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
TAGS

Atlassian Community Events