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() ;
new FlyingDelorean().Builder.speed(88).unit(Unit.MILES_PER_HOUR).build();
– Concent