Search code examples
javacalendardayofweek

Calculation of sum of days in java- android project


I have a non-zero integer value which is the sum of the days of the week where the days have the following values:

Sunday = 1 

Monday = 2

Tuesday = 4

Wednesday= 8 

Thursday = 16 

Friday = 32 

Saturday = 64

Example: Integer value 127 - All days of week, 65- only on sunday and saturday

I have seen few posts Convert integer to a list of week days regarding this, but I could not find any of the code in Java.

Depending on integer number i need to schedule task on particular day (sunday- saturday)


Solution

  • Are you asking how to determine which days were selected based on the value 127? If so, you can tell whether a given day was selected by doing an AND:

    int inputValue = 127;
    if (Monday & inputValue) {
      // Monday was selected
    }
    

    etc.

    Hard to be more specific without knowing your question.