Showing Morning, afternoon, evening, night message based on Time in java

后端 未结 15 1867
醉酒成梦
醉酒成梦 2020-12-14 01:39

What i am trying to do::

Show message based on

  • Good morning (12am-12pm)
  • Good after noon (12pm -4pm)
  • Good evening
15条回答
  •  醉酒成梦
    2020-12-14 01:54

    You should be doing something like:

    Calendar c = Calendar.getInstance();
    int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
    
    if(timeOfDay >= 0 && timeOfDay < 12){
        Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();        
    }else if(timeOfDay >= 12 && timeOfDay < 16){
        Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
    }else if(timeOfDay >= 16 && timeOfDay < 21){
        Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
    }else if(timeOfDay >= 21 && timeOfDay < 24){
        Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题