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

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

    For anyone who is looking for the latest Kotlin syntax for @SMA's answer, here is the helper function :

    fun getGreetingMessage():String{
        val c = Calendar.getInstance()
        val timeOfDay = c.get(Calendar.HOUR_OF_DAY)
    
        return when (timeOfDay) {
               in 0..11 -> "Good Morning"
               in 12..15 -> "Good Afternoon"
               in 16..20 -> "Good Evening"
               in 21..23 -> "Good Night"
               else -> "Hello"
          }
        }
    

提交回复
热议问题