# Java: Parts of the Day

*2017-03-16*

> A Java utility method that maps an hour from 0 to 23 to labels such as Early Morning, Afternoon, Evening, or Night.

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`).

```java
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](https://www.britannica.com/dictionary/eb/qa/parts-of-the-day-early-morning-late-morning-etc). 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` |

