Groovy script to check if today is a weekend...

1
0
-1

Hey Guys, I have a variable in my pool, data type text, called 'isWeekend'. As default, the variable is set to 'No'.

I'd like a script on a service to: 1) Get todays date. 2) Compare the date to see if it is a 'Saturday' or 'Sunday'. 3) Return 'Yes' if it is saturday or sunday, otherwise, 'No'.

Any pointers would be really great...

For bonus points, it would be great if the script yould return 'Yes' for any time outside of 9am - 5pm Monday - Friday. For Monday - Friday, it should say 'No'. But, any pointers for the question at the top would be a great help...

Thanks folks, Jim

3 answers

1
0
-1
This one is the BEST answer!

I think it needs to be along the lines of this:

def theday = 1 def mydate = new Date(system.currentTime())

theday = mydate.getDay()

if((theday == 6) or (theday ==7)){ return 'Yes' }else{ return 'No' }

Is this the rough direction?

Comments

Submitted by antoine.mottier on Mon, 03/16/2015 - 13:44

As method getDay() on java.util.Date is deprecated you can use the following alternative:

Calendar date = Calendar.getInstance();

String isWeekend = 'No';

int dayOfTheWeek = date.get(Calendar.DAY_OF_WEEK);

if((dayOfTheWeek == Calendar.SATURDAY) || (dayOfTheWeek == Calendar.SUNDAY)) {
        isWeekend = 'Yes';
}

return isWeekend;

1
0
-1

Hopefully you don't have to concern yourself with different jurisdictions...some people have weekends = Friday/Saturday. In your process what about Shift workers/part-time workers/flexitime workers etc....

Also consider Daylight savings...does that change as well?

Anyway - here is my code and I'm claiming the extra points for including the hours...

def mydate = new Date(System.currentTimeMillis())
int theExactDay
int theExactHour
def endResult = "No"

theExactDay = mydate.getDay();
theExactHour = mydate.getHours();
if(theExactDay == 0||theExactDay == 6){
        endResult = "Yes"}
else{
        if(9<theExactHour && theExactHour<17){
                //if between these numbers then Not IsWeekend
                endResult = "No"
                }
}
if(endResult.equalsIgnoreCase("Yes")){
        return "Yes"}
else{
        return "No"}

1
0
-1

I am using the below:

def mydate = new Date(System.currentTimeMillis()) int theExactDay def endResult = "No"

theExactDay = mydate.getDay(); if(theExactDay == 0){endResult = "Yes"} if(theExactDay == 6){endResult = "Yes"} if(endResult == "Yes"){return "Yes"} if(endResult == "No"){return "No"}

This seems to work. Does anybody have a better solution?

Comments

Submitted by rehab.zaky on Wed, 03/11/2015 - 15:42

Hi Guys, I can configure timer trigger for my task but how can I exclude weekends from sending notification ? Thanks a lot,

Notifications