How to handle full period in java.time?

In Java SE 8, it is the responsibility of the application to create a class linking Period and Duration if that is needed.

Note that a Duration contains an amount of seconds, not separate amounts of seconds, minutes and hours. The amount of seconds can exceed 24 hours, thus a Duration can represent a “day”. But it is a fixed 24 hours day. By contrast, the representation of a “day in Period is descriptive and takes into account DST. The state of a Period is formed from three separate fields – days, months and years.

Bear in mind that “The customer occupied the hotel room for 2 days and seventeen and a half hours, P2DT17H30M” has the possibility to be complicated by DST cutovers. Using Period and Duration separately things are clear – Period is affected by DST cutovers and Duration is not.

In design terms, the original java.time Period did include hours, minutes and seconds. However, this resulted in the need for many methods and complicated Javadoc to describe all the possibilities around normalization and DST. By separating the concepts, the interaction of each with the timeline is a lot clearer. Note that the two classes also relate to the SQL design (“year to month” and “day to second” concepts).

There are no current plans to add a new class for Java SE 9in this area, however it cannot be completely ruled out because XML/ISO-8601 allows a single combined representation.

Leave a Comment