in my app I’m updating some stuff if the time is between certain hours of the day which the user choose. It works fine if the user chooses something like “07-21”, but not with “21-07” which is over the night.
How I’m doing to check the time is I’m getting the current hour and converting it into milliseconds. Then I check if the current milli is between the chosen hours (those are also converted into milliseconds).
Like this:
if (currentMilli >= startHourMilli && currentMilli <= endHourMilli)
The problem: It doesn’t work if the user chooses anything that is over midnight (19-08 for example).
I’ve tried a lot of stuff but I just can’t figure out how to do this.
Any help is appreciated!
Do you increase the day of the year by 1 when you’re passing midnight? Otherwise your
startHourMillimight be greater thanendHourMilliand your if-clause will always be false.The solution is to use the
add-methodof the Calendar class. Therefore I calculate the interval’s length in hours and add this value to our Calendar instance.Let me know if this helps 🙂