How to get 5 years before now
According to the Javadoc, Instant will only accept temporal units from nanos to days Instant.plus(long amountToAdd, TemporalUnit unit);
You can use LocalDateTime. You use it the same way, but it will support operation on the YEARS level.
ZonedDateTime.now().minusYears(5).toInstant()
That will use your default time zone to compute the time. If you want another one, specify it in now()
. For example:
ZonedDateTime.now(ZoneOffset.UTC).minusYears(5).toInstant()