Java: Parts of the Day

·1 min read·paskov

This utility method returns a label for the part of the day, such as Morning, Afternoon, Evening, or Night. Pass the hour in 24-hour format (0 to 23).

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 these approximate parts-of-the-day definitions. It uses the following hour ranges, with both endpoints included:

Hour (24-hour clock) Returned value
5–8 Early Morning
9–10 Morning
11 Late Morning
12 Afternoon
13–15 Early Afternoon
16 Late Afternoon
17–19 Early Evening
20–21 Evening
22–23 and 0–4 Night