I'm taking a stab at it in what looks to me like something very logical.
Here is the code for the method plus(long, TemporalUnit)
(which is used in minus(...)
):
@Override
public Instant plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
case MILLIS: return plusMillis(amountToAdd);
case SECONDS: return plusSeconds(amountToAdd);
case MINUTES: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE));
case HOURS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR));
case HALF_DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2));
case DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY));
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
}
return unit.addTo(this, amountToAdd);
}
We can see that the results are calculated by multiplying seconds representation of units, a year cannot be logically and consistently represented by seconds for obvious reasons.
Addition
I can see another obvious reason why : constants used in the method above come from java.time.LocalTime
. The constants only define units up to days. No constant above days are defined (in LocalDate
and LocalDateTime
neither).
ChronoUnit.DAYS
is supported. That’s inconsistent… – SubjunctiveInstant.minus
directs toInstant.plus
which lists all supported units, further, the methodInstant.isSupported(TemporalUnit)
can be queried in advance and its documentation lists the supportedChronoUnit
s. – SubjunctiveInstant
would support anyTemporalUnit
whoseaddTo
method does the job, so you could implement an alternativeYEAR
unit performing the work, though this raises the question why to pass the request throughinstant.minus(…, YourYearUnit)
instead of invokingaddTo(instant, -…)
on your custom unit directly… – SubjunctiveThe Java Time-Scale divides each calendar day into exactly 86400 subdivisions, known as seconds. These seconds may differ from the SI second. It closely matches the de facto international civil time scale, the definition of which changes from time to time.
– BlackmanInstant
is UTC and thus assumed that it uses leap seconds. Thanks for pointing that out. The doc also says that if there is a day with a leap second, it will be spread equally over the seconds of the day, keeping the number of seconds per day of 86400. – Cicelycicenia@Holger
to tell me nothing new. We know what has been decided, but the question was why. – SubjunctiveInstant
is according to the documentation an implementation of the 'Java time-scale' and in that system. a day is the longest temporal unit defined (always as exactly 86.400 seconds). So the real question is most likely rather: Why was the 'Java time-scale' defined in such an arbitrary way with an obviously very incorrect definition of a day. – Ranket