How to add a certain number of hrs to an issue created time excluding the weekends and non-working hrs based on current user time zone to set a custom field values (datetime type) using a JMWE post function.
For example;
-If created time + 6hrs falls after 6pm on Friday, It should consider Monday 3pm
-If created time + 6hrs falls after 6pm on any weekdays , It should consider Next day 3pm
-If created time + 6hrs falls after before 9am on any week days, It should consider same day 3pm
import org.joda.time.DateTimeZone
import org.joda.time.DateTime
import com.atlassian.jira.timezone.TimeZoneManager
def CreatedTime = issue.get("created") + 6 // advance by 6 hours from created time
def now = CreatedTime.DateTime(DateTimeZone.forTimeZone(getComponent(TimeZoneManager).getTimeZoneforUser(currentUser)))
if (now.dayOfWeek > 5 || now.dayOfWeek == 5 && now.hourOfDay >= 18)
now = now.plusDays(7).withDayOfWeek(1).withTime(15,0,0,0) //move to Monday 3pm
else if (now.hourOfDay >= 18)
now = now.withTime(15,0,0,0).plusDays(1) //move to next day at 3pm
else if (now.hourOfDay < 9)
now = now.withTime(15,0,0,0) //same day 3pm
return now.toDate()
Somehow the code is unable to fetch created time with current time zone and use in if else conditions. Any help would be helpful. Thank you!
Hi @subham patra ,
I can see at least 2 problems in your code:
Something like this is probably what you want:
import org.joda.time.DateTimeZone
import org.joda.time.DateTime
import com.atlassian.jira.timezone.TimeZoneManager
def created = new DateTime(issue.get("created"),DateTimeZone.forTimeZone(getComponent(TimeZoneManager).getTimeZoneforUser(currentUser)))
def now = created.plusHours(6)
if (now.dayOfWeek > 5 || now.dayOfWeek == 5 && now.hourOfDay >= 18)
now = now.plusDays(7).withDayOfWeek(1).withTime(15,0,0,0) //move to Monday 3pm
else if (now.hourOfDay >= 18)
now = now.withTime(15,0,0,0).plusDays(1) //move to next day at 3pm
else if (now.hourOfDay < 9)
now = now.withTime(15,0,0,0) //same day 3pm
return now.toDate()
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.