Let's try something. I take the moment as ZonedDateTime and subtract 10 years in different time zones.
OffsetDateTime origin = OffsetDateTime.of(2018, 3, 1, 0, 0, 0, 0, ZoneOffset.UTC); Instant originInstant = origin.toInstant(); Instant tenYearsBackKyiv = origin.atZoneSameInstant(ZoneId.of("Europe/Kiev")) .minusYears(10) .toInstant(); long hoursSubtractedKyiv = ChronoUnit.HOURS.between(tenYearsBackKyiv, originInstant); System.out.println("Hours subtracted in Ρ: " + hoursSubtractedKyiv); Instant tenYearsBackSaoPaulo = origin.atZoneSameInstant(ZoneId.of("America/Sao_Paulo")) .minusYears(10) .toInstant(); long hoursSubtractedSaoPaulo = ChronoUnit.HOURS.between(tenYearsBackSaoPaulo, originInstant); System.out.println("Hours subtracted in SΓ£o Paulo: " + hoursSubtractedSaoPaulo);
Output:
Hours subtracted in Ρ: 87648 Hours subtracted in SΓ£o Paulo: 87672
As you can see, in Sao Paulo it is deducted 24 hours (1 day more) compared to Kiev (Kiev, Kiev). Perhaps you have already found out that this is because we go from March 1 to February 29 three times in leap years, only twice in Kiev.
The old and now obsolete Calendar class always had a time zone in it, so it knew in which time zone to subtract years (one more thing was happy to give you the result even in situations where it is not clear which result you wanted). The modern classes ZonedDateTime , OffsetDateTime and LocalDateTime can do the same. So use them. Instant does not conceptually have a time zone, so it refuses to perform operations depending on the time zone (I know that it is implemented using UTC, but we should consider this as an irrelevant implementation detail, and not as part of the class interface specification).
Neither the old nor the modern classes take into account the jump, and you are right, so Instant can add and subtract days, hours and minutes.
source share