0

我需要根据时间在文本视图上设置文本。如果不平等在一天之内(从 5:00 到 7:30),我使用:

if     (today.month == 7 &&
            today.monthDay == 7 &&
           (today.hour > 5 || (today.hour == 5 && today.minute >= 00)) &&
           (today.hour < 7 || (today.hour == 7 && today.minute <= 30)))
   {DubAcademy.setText("A");}

问题是这段代码在从一天到下一天的几个小时内都不起作用(即第二天的 23:00 到 1:00)。我试过这个,但没有工作

if     (today.month == 7 &&
            today.monthDay == 6 &&
           (today.hour > 23 || (today.hour == 23 && today.minute >= 00)) &&
           (today.monthDay <= 7 || (today.monthDay == 7 && today.hour <= 01 && today.minute <= 00 )))
   {DubAcademy.setText("B");}

我知道这是因为月份日期设置为 6,因此,当日期更改时,等式不再有效并且不显示文本。

有任何想法吗?

4

2 回答 2

0

尝试

if(today.month == 7 
    && ((today.monthDay == 6 && today.hour >= 23)
    ||  (today.monthDay == 7 && (today.hour < 1 || (today.hour == 1 && today.minute == 00)))

Today.minute 应该始终 >= 00 所以检查它似乎很愚蠢。

于 2014-08-06T23:40:40.930 回答
0

我的想法是使用 2 个Time对象作为更清晰代码的边界。将第一次设置为您想要的时间(对于直到第二天结束的范围,需要进行适当的修改)。然后相对于第一次设置第二次以简化操作。最后,调用normalize()以确保其值正确。然后你可以使用after()andbefore()检查时间是否在范围内。

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

Time t1 = new Time(today);
if(today.hour < 1){
    t1.monthDay--; // set date to yesterday
}
t1.hour = 23;
t1.minute = 0;
t1.second = 0;

Time t2 = new Time(t1);
t2.hour += 2;
t2.normalize(true);
// Android will ensure the value in each field are in range 

if(today.after(t1) && today.before(t2)){
    // do something here
}
于 2014-08-07T02:50:11.157 回答