gpt4 book ai didi

java - 如何计算两次休息一天之间的时间?

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:57 25 4
gpt4 key购买 nike

我有一个“请勿打扰”系统,如果当前时间在“请勿打扰”范围内,该系统会静音我的 Android 应用程序的声音。如果我使用一天之间的范围时间,效果很好,但我不知道如何在一天休息时写它,例如晚上 11:00 到第二天凌晨 1:00。

这是我用来检测免打扰时间的方法:

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);
}

最佳答案

请尝试下面的代码

 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);
}

关于java - 如何计算两次休息一天之间的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60320820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com