I'm using alarmmanager in android for set a repeating alarm for days selected by the user, that's works fine but if actual day is Sunday that's don't work correct. Let's see. If today's Sunday(7) and I set the alarm for Monday (8) and Tuesday(9) when I see the calendar toString() the date configured is 7, don't change, but if today is other day it's works fine only happens when today's Sunday.
calendar.set(Calendar.HOUR_OF_DAY,h)
calendar.set(Calendar.MINUTE,m)
calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY)//repeating for Monday if today is Sunday 8, and I set MONDAY, the new date most be Monday 9
Log.d("test",calendar.toString())
What's happening?
java.time through desugaring
Consider using java.time, the modern Java date and time API, for your date and time work.
Output when running just now (Monday, February 8):
If you wanted the alarm to happen already today, use
nextOrSame()instead ofnext().What went wrong in your code?
While it’s quite common for programmers to run into one of the many confusing points about the
Calendarclass, you have run into two of them:calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY)may set the date of theCalendarobject either to the previous or to the next Monday. Which one it picks does not only depend of the day of week but also of the settings of theCalendarobject, which will typically be different on different devices.calendar.toString(), the string you get will not reflect the necessary changes to theCalendarobject after changing the day of week. I would immediately expect that the day of week has changed, but the old value of day of month will still be printed. I am not going to study the documentation well enough to give you a precise answer. TheCalendardoes not compute its fields until you callget()or some other designated method that causes the recomputation to take place.There are plenty of reasons for not using
Calendar.Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bpwith subpackages.Links
java.timewas first described.java.timeto Java 6 and 7 (ThreeTen for JSR-310).