I have a Do Not Disturb system that mutes the sounds of my android app if the current time is in Do not disturb range time. It works fine if I use a range time just between a day, but I dont know how to write it with one day off, For example at 11:00 pm to 1:00 am of the next day.
This is method that I used for detecting DND time:
private boolean isInDNDTime() {
Calendar now = Calendar.getInstance();
Calendar startTime = Calendar.getInstance();
Calendar endTime = Calendar.getInstance();
MyDate myDate = new MyDate(new Date());
if (isDNDTwoDays()) {
startTime.setTime(myDate.getYesterday().toDate());
startTime.set(Calendar.HOUR_OF_DAY, getDNDStartHourTime());
startTime.set(Calendar.MINUTE, getDNDStartMinuteTime());
endTime.setTime(myDate.getTomorrow().toDate());
endTime.set(Calendar.HOUR_OF_DAY, getDNDEndHourTime());
endTime.set(Calendar.MINUTE, getDNDEndMinuteTime());
} else {
startTime.set(Calendar.HOUR_OF_DAY, getDNDStartHourTime());
startTime.set(Calendar.MINUTE, getDNDStartMinuteTime());
endTime.set(Calendar.HOUR_OF_DAY, getDNDEndHourTime());
endTime.set(Calendar.MINUTE, getDNDEndMinuteTime());
}
return now.after(startTime) && now.before(endTime);
}