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

后端 未结 15 1879
醉酒成梦
醉酒成梦 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 02:04

    I would shorten your if/elseif statement to:

    String greeting = null;
    if(hours>=1 && hours<=12){
        greeting = "Good Morning";
    } else if(hours>=12 && hours<=16){
        greeting = "Good Afternoon";
    } else if(hours>=16 && hours<=21){
        greeting = "Good Evening";
    } else if(hours>=21 && hours<=24){
        greeting = "Good Night";
    }
    Toast.makeText(this, greeting, Toast.LENGTH_SHORT).show();
    

提交回复
热议问题