You need to find out how many days between days, for example, Friday (constant number 6, Calendar.FRIDAY ) and Monday (constant number 2, Calendar.MONDAY ).

I write a repeating alarm clock under the android, in the database are the numbers for which days the alarm clock should be played, say 246 (Mon, Wed, Fri). If today is Friday, we need to set the alarm on Mon, therefore the difference in days is 3 days. How to learn it programmatically?

  • 6-2 not working? =) - Sergey Snegirev
  • one
    And from Monday to next Monday you have 7 days or 0? And in general, subtract the numerical values, if it turns out negative, add 7. - VladD
  • @SergeySnegirev and if today is Friday? =) - Dennis Zinkovsky

3 answers 3

(День2 - День1 ) if it is less than 0, then + 7

 ((День2 - День1) + 7) (mod 7) 

    Choose from these options:

     (a - b + 7) % 7 (a - b + 6) % 7 + 1 
       DateTime dateTimeS = new DateTime(2017, 4, 3, 0, 0); DateTime dateTimeE = new DateTime(2017, 4, 11, 0, 0); Period period = new Period(dateTimeS, dateTimeE); System.out.println( period.getDays()+ ":"+period.getMonths()+ ":"+period.getYears()); 
      • 2
        Why is the code here from your question? - vp_arth