Why does Joda Time allow a Period constructor to take two LocalTimes but there is no Duration constructor like that?
I want to know because it may aid in my understanding of the best use of Joda Time.
Here's my thinking: Duration is good 开发者_JAVA技巧for social convention unaware applications and the lack of awareness is what makes it different from Period. LocalTime is good for convention unaware use because it has no timezone. This suggests Duration should be used with LocalTime and vice-versa.
A Duration
is the amount of time between two precise Instant
s in time (which are completely independent of human concepts like years, days, and seconds). LocalTime
s, however, represent ambiguous points in time (they require a date and time zone to define an Instant
).
It makes sense to say there's a "standard duration" (the duration of the period assuming no DST, no leap years, no leap seconds (which Joda chronologies don't support, admittedly)) between two LocalTime
s, but there's not enough information to compute a "true duration". I suspect this is why Duration
doesn't have that constructor.
For example, let's suppose we have two LocalTimes
: one representing 1:00 AM and 4:00 AM. 99% of the time, it'd make sense to say that's a duration of 3 hours. But, if they represent times on the day of a Daylight Saving Time switch, the duration would be 2 hours or 4 hours.
Period
is defined more broadly, where it make senses to have a period between two partially-defined times like LocalTime
s (just "3 hours" in the example). Period
s correspond easily to standard durations, so one can just call period.toStandardDuration()
.
精彩评论