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

后端 未结 15 1846
醉酒成梦
醉酒成梦 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:05

    A cleaner method for detecting date would be.

    //the time cannot go below zero, so if this case is true, it must be between 0 and 12
       if(time <= 12)
       {
           return "Good Morning";
           //it will only fall into this case if the time is greater than 12.
       }else if(time < 16)
       {
           return "Good Afternoon";
       }else if(time < 21)
       {
           return "Good Evening";
       }else //it is guaranteed that the time will not exceed 24
           //, and if the previous case are all false, it must be within 21 and 24
       {
           return "Good Night";
       }
    

提交回复
热议问题