【问题标题】:Date inequality is not functioning in android日期不平等在android中不起作用
【发布时间】:2025-12-18 13:35:01
【问题描述】:

我想在 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),所以没有文字显示。有什么想法吗?

【问题讨论】:

    标签: android datetime textview timezone inequality


    【解决方案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) {
      ...
    }
    

    【讨论】: