天不工作之间的时间不平等

时间:2014-08-06 23:16:08

标签: android time textview inequality

我需要根据时间在文本视图上设置文本。 如果不平等在一天之内(从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,因此,当日期更改时,等式不再有效且不显示任何文本。

有什么想法吗?

2 个答案:

答案 0 :(得分: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应始终为&gt; = 00所以检查它似乎很愚蠢。

答案 1 :(得分:0)

我的想法是使用2个Time个对象作为清洁代码的边界。将第一次设置为您想要的时间(对于完成直到第二天的范围,需要进行适当的修改)。然后相对于第一个设置第二次以简化操作。最后,请致电normalize()以确保其值正确无误。然后,您只需使用after()before()来检查时间是否在范围内。

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
}