Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't time included in Java's Period class? [duplicate]

The Period class in java.time handles only the date-oriented potion: years, months, days.

What about the time portion: hours, minutes, seconds?

How can we parse and generate string representations of full periods as defined in ISO 8601, PnYnMnDTnHnMnS? For example, a day and a half: P1DT12H. The academic year is nine months, P9M. Every year I get two weeks and 3 days of vacation, P17D. The customer occupied the hotel room for 2 days and seventeen and a half hours, P2DT17H30M.

The Period class in Joda-Time handles full period. Why not in java.time? Is there some other mechanism?

like image 964
Basil Bourque Avatar asked Sep 03 '15 02:09

Basil Bourque


2 Answers

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.

like image 144
JodaStephen Avatar answered Oct 01 '22 22:10

JodaStephen


org.threeten.extra.PeriodDuration

The ThreeTen-Extra project offers a class combining a Period and a Duration. Simply called PeriodDuration.

An amount of time in the ISO-8601 calendar system that combines a period and a duration.

This class models a quantity or amount of time in terms of a Period and Duration. A period is a date-based amount of time, consisting of years, months and days. A duration is a time-based amount of time, consisting of seconds and nanoseconds. See the Period and Duration classes for more details.

The days in a period take account of daylight saving changes (23 or 25 hour days). When performing calculations, the period is added first, then the duration.

Caveat: Be sure to read the Answer by JodaStephen to understand the issues involved in trying to combine Period and Duration. It rarely makes sense to do so in practice, though that is counter to our intuition.

like image 44
Basil Bourque Avatar answered Oct 01 '22 21:10

Basil Bourque