如何知道现在时间是否在两个小时之间?

2022-01-15 00:00:00 time format android java hour

我现在有时间:

new Date();

我有一些小时常数,例如,23 和 8(晚上 11 点或 23:00、上午 8 点或 08:00).我怎么知道现在是两个小时常数之间的时间?

And I have some hour constants, for example, 23 and 8 (it's 11pm or 23:00, 8am or 08:00). How I can know is now time between it's two hour constants?

如果现在时间在两个小时之间,则需要运行一些程序代码或不运行,例如,如果已经是晚上并且不是早上,则不要运行一些代码.

这里的图片可以更好地解释:

Here the image to better explain:

静音模式不触发的某些情况:

00:00 20.06.13 - 23:00 20.06.13 // after 23.00 can loud!!

23:00 20.06.13 - 15:00 20.06.13 // after 15.00 can loud!!

01:00 20.06.13 - 08:00 20.06.13 // after 08.00 can loud!!

21:00 20.06.13 - 08:00 20.06.13 // after 08.00 can loud!!

推荐答案

试试这个

    int from = 2300;
    int to = 800;
    Date date = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int t = c.get(Calendar.HOUR_OF_DAY) * 100 + c.get(Calendar.MINUTE);
    boolean isBetween = to > from && t >= from && t <= to || to < from && (t >= from || t <= to);

相关文章