0

我想在 20:45 到 23:15 之间显示文本

    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();

if ((today.hour>=20 && today.minute>=45) 
               && (today.hour<=23 && today.minute<=15) ){
           MainTextView.setText("my text");}

问题是这样分钟会互相干扰(实际上不可能小于15同时大于45),所以没有显示文本。有任何想法吗?

4

1 回答 1

1

是的,你只需要修正你的逻辑。如果小时是临界小时,您只需要比较分钟。例如:

if ((today.hour > 20 || (today.hour == 20 && today.minute >= 45)) &&
    (today.hour < 23 || (today.hour == 23 && today.minute <= 15)) {
  ...
}

或者,将时间转换为“一天中的分钟数”并基于此进行算术:

int minuteOfDay = today.hour * 60 + today.minute;
if (minuteOfDay >= 20 * 60 + 45 &&
    minuteOfDay <= 23 * 60 + 15) {
  ...
}
于 2014-08-04T21:30:45.397 回答