Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 5 years before now

Tags:

java

java-8

I am new to java 8 and I am trying to get five years before now, here is my code:

Instant fiveYearsBefore = Instant.now().plus(-5,                     ChronoUnit.YEARS); 

But I get the following error:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years 

Can anyone help me how to do that?

like image 962
HMdeveloper Avatar asked Jan 15 '16 14:01

HMdeveloper


People also ask

What was the year 50 years ago?

1968: 50 years ago | Smithsonian Institution.

How many days are in a year?

This year is 365 days, 6 hours, 9 minutes and 9 seconds, or about 365.26 days long.

What is the 90th day from today?

90 days from now Today is August 27, 2022 so that means that 90 days from today would be November 25, 2022.


1 Answers

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() 
like image 108
JB Nizet Avatar answered Oct 19 '22 09:10

JB Nizet