Listing the Time Zones with the nearest previous midnight moments on Java8

lovely color scheme

I can tell you that the code snippet will be shorter than the title ….

Recently I needed to run a daemon , for every user in the product , just after every midnight. The problem is , every user has their own time zones and their own midnight moments. Turns out that , calculating , or mostly figuring out which time zones just passed their midnight moment , or simply moved to the next day , at a given moment, is pretty complicated and mind boggling. Especially when you take the wrong path of calculations , like I once did.

But with Java 8 and plus , you can list them easily with a few lines of code. Usually takes under 50ms.

We simply traverse the zoneId s provided by the ZoneId.getAvailableZoneIds() method and feed it to a parallelStream, setting our Instant to each TimeZone and see If the hour is “00” at that time zone.

The zone Ids are Strings , which can be also previewed in detail and with extra information here on wikipedia , compatible with the tz database.

I love how the tiny Island nations mess up the order XD