Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Calendar.HOUR\u OF_DAY的条件语句_Java_Android - Fatal编程技术网

Java 使用Calendar.HOUR\u OF_DAY的条件语句

Java 使用Calendar.HOUR\u OF_DAY的条件语句,java,android,Java,Android,我有这个代码,它必须根据一天中的时间显示一个字符串。 代码是 public void onTimeTick() { mTime.setTimeInMillis(System.currentTimeMillis()); // Let's see what string we need according to the time int saluteResId = R.string.salute_fallback; if (mTime.get(Calendar.H

我有这个代码,它必须根据一天中的时间显示一个字符串。 代码是

public void onTimeTick() {
    mTime.setTimeInMillis(System.currentTimeMillis());

    // Let's see what string we need according to the time
    int saluteResId = R.string.salute_fallback;

    if (mTime.get(Calendar.HOUR_OF_DAY) > 4) {
        saluteResId = R.string.salute_morning;
    } else if (mTime.get(Calendar.HOUR_OF_DAY) > 12) {
        saluteResId = R.string.salute_evening;
    } else if (mTime.get(Calendar.HOUR_OF_DAY) > 19 ||  mTime.get(Calendar.HOUR_OF_DAY) < 5) {
        saluteResId = R.string.salute_night;
    }

}

但问题是,一天中的小时数始终大于4,因此它甚至不会检查剩余的两个条件,字符串将始终设置为向早晨致敬。我对java不是很在行,我试图找出如何将第一个条件设为false,以便它检查其他条件并根据它们设置字符串。

我认为这可能会有所帮助

 if (mTime.get(Calendar.HOUR_OF_DAY) > 4 && mTime.get(Calendar.HOUR_OF_DAY) <= 12) {
     //saluteResId = R.string.salute_morning;
 } else if (mTime.get(Calendar.HOUR_OF_DAY) > 12 && mTime.get(Calendar.HOUR_OF_DAY) <= 19) {
     //saluteResId = R.string.salute_evening;
 } else if (mTime.get(Calendar.HOUR_OF_DAY) > 19 &&  mTime.get(Calendar.HOUR_OF_DAY) <= 4) {
     //saluteResId = R.string.salute_night;
 }
试试这个代码

final Calendar d = Calendar.getInstance();
        
        
        final int hh = d.get(Calendar.HOUR_OF_DAY);
        
        
        //try this way


        String time = null;

        if (hh == 0) {
   
            time =("Salute_Night");
        } else if (hh == 1) {
            
            time =("Salute_Night");
        } else if (hh == 2) {
            
            time =("Salute_Night");
        } else if (hh == 3) {
            
            time =("Salute_Night");
        } else if (hh == 4) {
            
            time =("Salute_Night");
        } else if (hh == 5) {
           
            time =("Salute_Morning");
        } else if (hh == 6) {
            
            time =("Salute_Morning");
        } else if (hh == 7) {
           
            time =("Salute_Morning");
        } else if (hh == 8) {
           
            time =("Salute_Morning");
        } else if (hh == 9) {
            
            time =("Salute_Morning");
        } else if (hh == 10) {
           
            time =("Salute_Morning");
        } else if (hh == 11) {
           
            time =("Salute_Morning");
        } else if (hh == 12) {
            
            time =("Salute_Evening");
        } else if (hh == 13) {
            
            time =("Salute_Evening");
        } else if (hh == 14) {
            
            time =("Salute_Evening");
        } else if (hh == 15) {
            
            time =("Salute_Evening");
        } else if (hh == 16) {
            
            time =("Salute_Evening");
        } else if (hh == 17) {
            
            time =("Salute_Evening");
        } else if (hh == 18) {
            
            time =("Salute_Evening");
        } else if (hh == 19) {
            
            time =("Salute_Evening");
        } else if (hh == 20) {
            
            time =("Salute_Night");
        } else if (hh == 21) {
            
            time =("Salute_Night");
        } else if (hh == 22) {
            
            time =("Salute_Night");
        } else if (hh == 23) {
            
            time =("Salute_Night");
        }
       
//your text view where you want it to display

textviewTimeOfDay.settext.(time);


如果以相反的顺序运行If检查,则>4的检查不会超过>12的检查。以此类推,从>19开始,然后>12,最后>4等等,就是这样。。该死的,我真的想不起改变命令。非常感谢大家,我现在99%确信它会起作用。仅供参考,您使用的日期时间类很糟糕,多年前被JSR 310中定义的java.time类所取代。我不太擅长java,但我可以说这不是最好的方法。@Priyanshu是的,您可以缩短代码。但这很简单,对我来说效果很好。