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

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

    More specific

    public static String getDayMessage() {
        Calendar c = Calendar.getInstance();
        int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
    
        if (timeOfDay < 5) {
            return "Hi, Good Mid Night";
        }else if (timeOfDay < 6) {
            return "Hi, Good Late Night";
        } else if (timeOfDay < 12) {
            return "Hi, Good Morning";
        } else if (timeOfDay < 14) {
            return "Hi, Good Noon";
        } else if (timeOfDay < 16) {
            return "Hi, Good Afternoon";
        } else if (timeOfDay < 21) {
            return "Hi, Good Evening";
        } else {
            return "Hi, Good Night";
        }
    }
    

提交回复
热议问题