How to obtain the start time and end time of a day?
To easily get the start and end times of the present day, use java.time.LocalDate
and java.time.LocalTime
. Here is the quick code:
The function atStartOfDay
gives you the start of the day (00:00), while LocalTime.MAX
gives you the millisecond before the next day begins (23:59:59.999).
Handling edge cases and advanced tricks
Juggling multiple time zones
If you're managing time zones, it's worth pinpointing ZoneId
. Check the code below:
Navigating the old Java sea
For those sailing in the Java 7 and earlier sea, use robust libraries like Apache Commons Lang, or stick to manual setting:
To the millisecond and beyond!
For those applications craving for millisecond precision, here's how to quench their thirst:
Maintaining consistency across all apps
For accurate and consistent time tracking, all applications should follow the same definitions:
- Stick to
LocalTime.MIN
andLocalTime.MAX
- Stick to standard zone offsets for global applications
- Stick to using
Instant
for applications logged and timestamped across systems
Handling intervals like a boss
To ace your period management game, go with the Interval class:
Checking if a date falls within a certain period
Determine if a specific date
is within certain bounds as shown below:
Was this article helpful?