Description
Some methods that accept an interface such as Temporal will do an “instanceof” check and throw an exception if the two classes do not match.
A reasonable effort should be made to honor the interface before throwing an exception.
For example ZonedDateTime, Instant, and OffsetDateTime can all represent a point on the timeline.
If calculating the amount of time between an Instant and a OffsetDateTime, you’d have to use the “from()” method to cast one of the objects before calculating the time between them. Not doing so will result in an exception being thrown at runtime.
Interfaces should be honored as much as possible otherwise poor coding patterns such as the following could emerge in code that uses the java.time api.
if(param instanceof LocalDateTime){
finalParam = param;
} else {
finalParam = LocalDateTime.from(param);
}
See JSR 310 issue: 328 https://github.com/ThreeTen/threeten/issues/328
A reasonable effort should be made to honor the interface before throwing an exception.
For example ZonedDateTime, Instant, and OffsetDateTime can all represent a point on the timeline.
If calculating the amount of time between an Instant and a OffsetDateTime, you’d have to use the “from()” method to cast one of the objects before calculating the time between them. Not doing so will result in an exception being thrown at runtime.
Interfaces should be honored as much as possible otherwise poor coding patterns such as the following could emerge in code that uses the java.time api.
if(param instanceof LocalDateTime){
finalParam = param;
} else {
finalParam = LocalDateTime.from(param);
}
See JSR 310 issue: 328 https://github.com/ThreeTen/threeten/issues/328