Check if DAY_OF_WEEK is between Monday and Friday

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.

  • What is Neon with respect to Android?
  • PopUp dialog Android from background thread
  • How to make an Android view that flips between views on swipe/fling
  • IntentService won't show Toast
  • Secret Android build process
  • Positioning an image inside an ImageView with max height and max width set
  • 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;
                    }
    

    Related posts:

    Random app crashes on Android Wear 5.0.X
    How to fix &#039;cordova&#039; is not recognized in command line - windows 8?
    Can PackageManager.getInstallerPackageName() tell me that my app was installed from Amazon app store...
    A Switch Java problem : case expressions must be constant expressions
    android How to move a map under a marker
    Can&#039;t stream videos
  • FragmentTabHost graphical layout doesn't render
  • Recyclerview with endless scrolling in nestedscrollview, triggers onScrolled
  • `Leaderboard` tab not showing in Google Play Games while `Achievements` tab is there
  • Android - Can't create handler inside thread that has not called Looper.prepare()
  • Why RecyclerView.OnScrollListener is not an interface but an abstract class?
  • Nine-patch images for web development
  • 5 Solutions collect form web for “Check if DAY_OF_WEEK is between Monday and Friday”

    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
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.