I found that Java 8 doesn't have an equivalent to ZonedDateTime but to work only with Time (a ZonedTime class or something like that). I know they included the OffsetTime class, but it only stores the offset.
Storing time zones along with date and time, instead of just store the offset, helps to deal with daylight savings easier.
I'm not asking you to give me alternatives, I know there are many approaches; I'm just wondering why such class was not included, is it a design issue? or they just found it to be redundant?
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.
Use OffsetDateTime to store unique instants in the universal timelines irrespective of the timezones, such as keeping the timestamps in the database or transferring information to remote systems worldwide. Use ZonedDateTime for displaying timestamps to users according to their local timezone rules and offsets.
Therefore, we should always prefer storing OffsetDateTime in the database over the ZonedDateTime, as dates with a local time offset always represent the same instants in time. Moreover, unlike with the ZonedDateTime, adding an index over a column storing the OffsetDateTime won't change the meaning of the date.
A LocalDateTime instance represents a point in the local timeline. It cannot represent an instant on the universal timeline without additional information such as an offset or time zone. A ZonedDateTime instance represents an instant in the universal timeline. It is the combination of date, time and zone information.
Introducing a class ZonedTime
consisting of LocalTime
and a time zone (instead of a simple offset) would be a heavy mistake.
The suggested type does not contain a date. But without a date it will not be possible to evaluate the real timezone offset to map the local time to any sensible global time. For taking into account any daylight saving aspects you must have as well a date and a time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With