68. Defining a day period
Let’s imagine that we want to say hello to a friend from another country (in a different time zone) via a message such as Good morning, Good afternoon, and so on based on their local time. So, having access to AM/PM flags is not enough, because we consider that a day (24 hours) can be represented by the following periods:
- 9:00 PM (or 21:00) – 5:59 AM = night
- 6:00 AM – 11:59 AM = morning
- 12:00 PM – 5:59 PM (or 17:59) = afternoon
- 6:00 PM (or 18:00) – 8:59 PM (or 20:59) = evening
Before JDK 16
First, we have to obtain the time corresponding to our friend’s time zone. For this, we can start from our local time given as a java.util.Date
, java.time.LocalTime
, and so on. If we start from a java.util.Date
, then we can obtain the time in our friend’s time zone as follows:
LocalTime lt = date.toInstant().atZone(zoneId).toLocalTime();
Here, date
is a new Date()
and zoneId...