HMdeveloper
HMdeveloper

Reputation: 2884

How to get 5 years before now

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?

Upvotes: 67

Views: 40783

Answers (6)

Basil Bourque
Basil Bourque

Reputation: 340118

tl;dr

LocalDate                            // Represent a date only, no time, no zone.
.now( 
    ZoneId.of( "America/Edmonton" )  // Today's date varies by zone.
)
.minusYears( 5 )                     // Go back in time by years.
.toString()                          // Generate text is standard format.

Run code at Ideone.com.

2018-11-20

For your copy-paste convenience:

LocalDate.now().minusYears( 5 )  // Using JVM’s current default time zone to get today's date.

LocalDate

If you want simply the date, without time-of-day, and without time zone, use LocalDate. To move back in years, call minusYears.

ZoneId

To capture the current date, specify a time zone. If omitted, the JVM’s current default time zone is implicitly applied. Understand that for any given moment, the date varies around the globe by time zone. It's "tomorrow" in Asia/Tokyo while still "yesterday" in America/Edmonton.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
LocalDate today = LocalDate.now( z ) ;
LocalDate fiveYearsAgo = today.minusYears( 5 ) ;

FYI, see list of real time zone names, with Continent/Region names.

Generate text

Generate text is standard ISO 8601 format.

String output = fiveYearsAgo.toString() ;

Upvotes: 0

Reneta
Reneta

Reputation: 516

I bumped into the same exception, but with ChronoUnit.MONTHS. It is a little bit misleading, because on compile time does not throw an error or warning or something. Anyway, I read the documentation, too: enter image description here

and, yes, all the other ChronoUnit types are not supported unfortunately.

Happily, LocalDateTime can substract months and years, too.

LocalDateTime.now().minusYears(yearsBack)

LocalDateTime.now().minusMonths(monthsBack);

Upvotes: 1

Usman Ismail
Usman Ismail

Reputation: 18679

If you are so inclined is does not require the date time conversion.

Instant.now().minus(Period.ofYears(5).getDays(),ChronoUnit.DAYS);

Upvotes: -6

Milan
Milan

Reputation: 1910

Instant does not support addition or subtraction of YEARS.

You can use this LocalDate if you only need date without time:

LocalDate date = LocalDate.now();
date = date.plus(-5, ChronoUnit.YEARS);

Otherwise you can user LocalDateTime.

Upvotes: 9

pca
pca

Reputation: 524

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.

Upvotes: 18

JB Nizet
JB Nizet

Reputation: 692181

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()

Upvotes: 91

Related Questions