1

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);
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Sina
  • 53
  • 6
  • Just distinguish between the two cases. Same day is "after start AND before end", different day is "after start OR before end" – kumesana Feb 20 '20 at 13:18
  • How have you tried it so far? What didn't work? Which is the smallest API level your app is supporting? – deHaar Feb 20 '20 at 13:19
  • @deHaar I edited the question, And the smallest API level for my app is Lollipop (21). – Sina Feb 20 '20 at 13:25

2 Answers2

0

Pls try below code

 private boolean isInDNDTime() {
        Calendar now = Calendar.getInstance();
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        MyDate myDate = new MyDate(new Date());

        if (isDNDTwoDays()) {
            startTime.add(Calendar.DATE, 1);
            endTime.add(Calendar.DATE, 1);

           // 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);
    }
Manish
  • 1,071
  • 2
  • 10
  • 27
0

There is a modern API for tasks like this, it is called java.time and is available from Java 8. The following example illustrates how to do that with a few lines of code:

public static void main(String[] args) {
    // create sample data
    LocalDateTime start = LocalDateTime.of(2020, 2, 19, 12, 30);
    LocalDateTime timeInDND = LocalDateTime.now();
    LocalDateTime end = LocalDateTime.of(2020, 2, 21, 12, 30);

    // just check if the time is equal to start or end or is between them
    if (timeInDND.equals(start) || timeInDND.equals(end)
            || (timeInDND.isAfter(start) && timeInDND.isBefore(end))) {
        System.out.println(timeInDND.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) 
                + " is in the DND period");
    } else {
        System.err.println(timeInDND.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) 
                + " is not in the DND period");
    }
}

Unfortunately, your support of Android API levels below 26 requires an external library, the ThreeTen Android Backport because java.time is available from API level 26. You can check another question about how to use the ThreeTenABP.

deHaar
  • 17,687
  • 10
  • 38
  • 51