Check if DAY_OF_WEEK is between Monday and Friday

95,136

Solution 1

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.

Solution 2

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.

Solution 3

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;
}

Solution 4

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

Solution 5

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!!

Share:
95,136

Related videos on Youtube

safari
Author by

safari

Welcome on my Profile I'm 20 years old, probably not a bad Android programmer and now just started with IOS.

Updated on November 09, 2020

Comments

  • safari
    safari over 3 years

    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;
                    }
    
    • Kumar Bibek
      Kumar Bibek about 12 years
      Why can't you just check if day is between Calendar.MONDAY(value 2) and Calendar.FRIDAY(value 6) ?
    • safari
      safari about 12 years
      the DAY_OF_WEEK, has to be one of those 5 day's
    • L7ColWinters
      L7ColWinters about 12 years
      no, its because you have to Calendar.get(Calendar.DAY_OF_WEEK);
  • likejudo
    likejudo about 10 years
    why does it start at 1? It should start at 0. this makes it more awkward to do expressions like while((today = ++today % 7) != startingPoint
  • paxdiablo
    paxdiablo almost 9 years
    @likejiujitsu: I suspect it starts at one since no-one other than uber-nerds would use the phrase "zeroth day of the week". Think of it from the other side of the fence, all us poor C coders who have to continuously add one the tm_mon when outputting dates :-) It's easy enough in your case to do while ((today = (today % 7) + 1) != startingPoint) but, yes, you have to think about it a bit.
  • cyberPrivacy
    cyberPrivacy about 8 years
    Correct! This is important if you want to get it work properly!
  • Nicholas Hamilton
    Nicholas Hamilton about 6 years
    lol "that's like trying to kill a mosquito with a thermo-nuclear warhead", classic.