Java: Parts of the Day

A handy utility method that will return the part of the day, such as morning, afternoon, evening, etc.

public static String getPartOfTheDay(final int hour)
{
    if(hour > 4 && hour < 12)
    {
        if(hour <= 8)
        {
            return "Early Morning";
        }
        else if(hour > 8 && hour < 11)
        {
            return "Morning";
        }
 
        return "Late Morning";        
    }
    else if(hour >= 12 && hour < 17)
    {
        if(hour >= 13 && hour <= 15)
        {   
            return "Early Afternoon";
        }
        else if(hour >= 16)
        {
            return "Late Afternoon";
        }
 
        return "Afternoon";
    }
    else if(hour >= 17 && hour <= 21)
    {
        if(hour <= 19)
        {   
            return "Early Evening";
        }
 
        return "Evening";
    }
    else
    {
        return "Night";
    }
}

The method is based on the following logic, that many people would agree with:

Morning: 5 to 12

  • Early morning: 5 to 8
  • Late morning: 11 to 12

Afternoon: 12 to 17

  • Early afternoon: 13 to 15
  • Late afternoon: 16 to 17

Evening: 17 to 21

  • Early evening: 17 to 21

Night: 21 to 4

Leave a Reply

Your email address will not be published. Required fields are marked *

*