java – Check if DAY_OF_WEEK is between Monday and Friday-ThrowExceptions

Exception or error:

I’m trying to create a method which is checking if “today” is between Monday and Friday. For this I get with this line 'int day = Calendar.DAY_OF_WEEK;' the actual day. After that I fill a ArrayList with the days (Monday, Tuesday, Wendsday, Thursday and Friday). Now when I check if the actual day is in my ArrayList, i set boolean DAY = true else i set boolean DAY = false. I tryed the Method today and yesterday, but it allways sets the boolean to false.

What do I need to change that my code works? You’ll find the code down here.

Code

                int day = Calendar.DAY_OF_WEEK;
                ArrayList<Integer> daylist = new ArrayList<Integer>();
                daylist.add(Calendar.MONDAY);
                daylist.add(Calendar.TUESDAY);
                daylist.add(Calendar.WEDNESDAY);
                daylist.add(Calendar.THURSDAY);
                daylist.add(Calendar.FRIDAY);

                if (daylist.contains(day)){
                    DAY = true;
                }else{
                    DAY = false;
                }
How to solve:

Wow, that’s like trying to kill a mosquito with a thermo-nuclear warhead 🙂

Java guarantees (in 1.5) (unchanged up to 1.8 at least) that the values of SUNDAY through SATURDAY are contiguous (1 through 7) so it’s a simple matter of checking a range.

However, DAY_OF_WEEK is not the day of the week, it’s a field number (with the value 7) to be passed to the getter to retrieve the day of the week. The only time Calendar.DAY_OF_WEEK itself will match an actual day will be on Saturdays.

You can use code such as:

Calendar myDate = Calendar.getInstance(); // set this up however you need it.
int dow = myDate.get (Calendar.DAY_OF_WEEK);
boolean isWeekday = ((dow >= Calendar.MONDAY) && (dow <= Calendar.FRIDAY));

Following this, isWeekday will be true if and only if the day from myDate was Monday through Friday inclusive.

###

int day = Calendar.DAY_OF_WEEK; should instead be

Calendar cal; // The calendar object
....your other code for getting the date goes here....
int day = cal.get(Calendar.DAY_OF_WEEK);

Your current code just gets the value of the constant Calendar.DAY_OF_WEEK.

###

This should do the trick for you i assume.

int day = cal.get(Calendar.DAY_OF_WEEK);
if (day >= Calendar.MONDAY && day <= Calendar.FRIDAY){
    DAY = true;
}else{
    DAY = false;
}

###

First Calendar.DAY_OF_WEEK is an integer field will always gives you 7. You need to create an instance of a Calendar like Calendar cal = Calendar.getInstance(); By default it gives you the current date in current timezone.
Then you can call cal.get(Calendar.DAY_OF_WEEK); which will give you any day between Sunday and Sat’day

Now you can check something like this

    if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
         System.out.println("Weekend");
     } else {
        System.out.println("Weekday");
     }

You can apply this logic to your problem!!

###

int day = Calendar.DAY_OF_WEEK;

The logic is broken right here. DAY_OF_WEEK is a constant identifying which type of data we need to retrieve from a Calendar instance.

The simplest solution to your problem (since Calendar.FRIDAY > … > Calendar.MONDAY) is

Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_WEEK);
if (day >= Calendar.MONDAY && day <= Calendar.FRIDAY)
  // do something

Leave a Reply

Your email address will not be published. Required fields are marked *