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".
About Question enthuware.ocpjp.v17.2.3791
Moderator: admin
-
- Posts: 12
- Joined: Fri Sep 09, 2022 8:41 am
- Contact:
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3791
Thank you for your feedback. This will be updated asap.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 33 guests