Closed MenoData closed 9 years ago
After long investigation, the idea of static from(TemporalAccessor)
-methods is finally rejected. Main reason is the philosophy of Time4J to preserve type-safety as much as possible. A type declared just as TemporalAccessor
is not much better than using java.lang.Object
. Code like Moment.from(LocalDate)
or PlainTime.from(Instant)
would compile and cause a runtime exception (due to missing timezone). Such from(...)
-methods would also be in competition with the type-safe conversion class TemporalType
and are mainly motivated by parsing purposes (within the scope of JSR-310). The readability of method references like PlainDate::from
to be used in parsing context is rather suboptimal. But Time4J-types don't need the format engine of JSR-310 at all. Instead they should use the superior alternative ChronoFormatter
as much as possible.
Solved for v4.0. The main solution consists of several conversion constants in the central entry point TemporalType
. Finally some static from(...)
-methods were added to the four basic types and the class net.time4j.Duration
as short-cuts (but always with concrete parameter type instead of using TemporalAccessor
or TemporalAmount
!).
Update: The type conversions will be lenient regarding these two details.
java.time.Instant
)LocalTime
)Now the type conversions are more lenient. They are also used via the adapter interface ThreetenAdapter
, see issue #217
In order to realize the reverse conversion, classes like
PlainDate
etc. should introduce new static methods with the signaturepublic static PlainDate from(TemporalAccessor)
or similar.The class
net.time4j.Duration
might get new methods with the signaturespublic static Duration<U> from(TemporalAmount, Class<U>)
andpublic static Duration<ClockUnit> from(java.time.Duration)
andpublic static Duration<CalendarUnit> from(java.time.Period)
.