Page 1 of 1

About Question enthuware.ocpjp.v17.2.3791

Posted: Sat Oct 15, 2022 10:30 am
by theujior
the question explains
date1.compare(date2) will return second date minus first date in seconds i.e. -14400, which, if converted to hours will be -1.

At first, maybe
date1.getOffset().compareTo(date2.getOffset()) will return second offset minus first offset in seconds i.e. -3600, which, if converted to hours will be -1.

API Document of ZoneOffset.compareTo explains

Returns:
the comparator value, negative if less, positive if greater


It is typical returns of Comparable.compareTo(, although zero is missing).
It does not be required to return the difference between offsets in seconds.
The actual return values depend on system class libraries, the implementation of the Date and Time API.
(Sorry for wrong expression on About Question enthuware.ocpjp.v17.2.3577)

Maybe, with the class libraries based on OpenJDK (although I don't know another),
ZoneOffset.compareTo would return the difference between offsets in seconds.
But it is wrong to conclude that ZoneOffset.compareTo returns the difference between offsets in seconds.
Returns happened to be implemented so, and might be changed to, for example -1,0,1.

And order of ZoneOffset should be explained.
API Document of ZoneOffset.compareTo explains

Compares this offset to another offset in descending order.
The offsets are compared in the order that they occur for the same time of day around the world. Thus, an offset of +10:00 comes before an offset of +09:00 and so on down to -18:00.


It is the reason why second offset minus first offset, not first minus second.

In addition, I think that compareTo should not be used for any purposes other than comparison.

This question should be modified or remaked, not explain "it would not be a good idea to rely upon it".

Re: About Question enthuware.ocpjp.v17.2.3791

Posted: Mon Oct 17, 2022 12:43 am
by admin
Thank you for your feedback. This will be updated asap.