Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8, why not a ZonedTime class?

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?

like image 460
Juan Lhc Avatar asked Oct 23 '14 21:10

Juan Lhc


People also ask

What is a ZonedDateTime in the Java 8 date and time API?

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.

Should I use ZonedDateTime or OffsetDateTime?

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.

Should I use OffsetDateTime?

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.

Should I use LocalDateTime or ZonedDateTime?

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.


1 Answers

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.

like image 120
Meno Hochschild Avatar answered Sep 21 '22 08:09

Meno Hochschild