Java : Year with Examples

Year (Java SE 18 & JDK 18) with Examples.
You will find code examples on most Year methods.


Summary

A year in the ISO-8601 calendar system, such as 2007.

Class diagram

final var year = Year.of(1999);
System.out.println(year); // 1999

// Gets the current year.
final var now = Year.now();
System.out.println(now); // 2022

Fields

static final int MAX_VALUE

The maximum supported year, '+999,999,999'.

System.out.println(Year.MAX_VALUE); // 999999999

static final int MIN_VALUE

The minimum supported year, '-999,999,999'.

System.out.println(Year.MIN_VALUE); // -999999999

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have this year.

final var year = Year.of(1999);
System.out.println(year); // 1999

final var date = LocalDate.of(2001, 2, 3);
System.out.println(date); // 2001-02-03

final var ret = year.adjustInto(date);
System.out.println(ret); // 1999-02-03

LocalDate atDay (int dayOfYear)

Combines this year with a day-of-year to create a LocalDate.

final var year = Year.of(1999);
System.out.println(year); // 1999

System.out.println(year.atDay(2)); // 1999-01-02
System.out.println(year.atDay(3)); // 1999-01-03
System.out.println(year.atDay(32)); // 1999-02-01
System.out.println(year.atDay(365)); // 1999-12-31

// DateTimeException: Invalid date 'DayOfYear 366' as '1999' is not a leap year
//year.atDay(366);

YearMonth atMonth (int month)

Combines this year with a month to create a YearMonth.

final var year = Year.of(2000);
System.out.println(year); // 2000

System.out.println(year.atMonth(1)); // 2000-01
System.out.println(year.atMonth(Month.JANUARY)); // 2000-01

System.out.println(year.atMonth(2)); // 2000-02
System.out.println(year.atMonth(Month.FEBRUARY)); // 2000-02

System.out.println(year.atMonth(12)); // 2000-12
System.out.println(year.atMonth(Month.DECEMBER)); // 2000-12

// DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 13
//year.atMonth(13);

YearMonth atMonth (Month month)

Combines this year with a month to create a YearMonth.

Please see atMonth(int month).

LocalDate atMonthDay (MonthDay monthDay)

Combines this year with a month-day to create a LocalDate.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var monthDay1 = MonthDay.of(1, 2);
System.out.println(monthDay1); // --01-02
System.out.println(year.atMonthDay(monthDay1)); // 2000-01-02

final var monthDay2 = MonthDay.of(12, 31);
System.out.println(monthDay2); // --12-31
System.out.println(year.atMonthDay(monthDay2)); // 2000-12-31

int compareTo (Year other)

Compares this year to another year.

final var year1 = Year.of(2000);
final var year2 = Year.of(2000);

System.out.println(year1); // 2000
System.out.println(year2); // 2000

System.out.println(year1.compareTo(year2)); // 0
final var year1 = Year.of(1234);
final var year2 = Year.of(5678);

System.out.println(year1); // 1234
System.out.println(year2); // 5678

System.out.println(year1.compareTo(year2)); // -4444
final var year1 = Year.of(1234);
final var year2 = Year.of(-1234);

System.out.println(year1); // 1234
System.out.println(year2); // -1234

System.out.println(year1.compareTo(year2)); // 2468

boolean equals (Object obj)

Checks if this year is equal to another year.

final var year1 = Year.of(2000);
final var year2 = Year.of(2000);

System.out.println(year1); // 2000
System.out.println(year2); // 2000

System.out.println(year1.equals(year2)); // true
final var year1 = Year.of(1234);
final var year2 = Year.of(5678);

System.out.println(year1); // 1234
System.out.println(year2); // 5678

System.out.println(year1.equals(year2)); // false
final var year1 = Year.of(1234);
final var year2 = Year.of(-1234);

System.out.println(year1); // 1234
System.out.println(year2); // -1234

System.out.println(year1.equals(year2)); // false

String format (DateTimeFormatter formatter)

Formats this year using the specified formatter.

final var year = Year.of(2004);
System.out.println(year); // 2004

final var ret1 = year.format(DateTimeFormatter.ofPattern("yyyy"));
System.out.println(ret1); // 2004

final var ret2 = year.format(DateTimeFormatter.ofPattern("yy"));
System.out.println(ret2); // 04

static Year from (TemporalAccessor temporal)

Obtains an instance of Year from a temporal object.

final var date = LocalDate.of(2001, 2, 3);
System.out.println(date); // 2001-02-03

final var ret = Year.from(date);
System.out.println(ret); // 2001
final var dateTime = LocalDateTime.of(1999, 1, 1, 12, 30);
System.out.println(dateTime); // 1999-01-01T12:30

final var ret = Year.from(dateTime);
System.out.println(ret); // 1999

int get (TemporalField field)

Gets the value of the specified field from this year as an int.

final var year = Year.of(2000);
System.out.println(year); // 2000

System.out.println(year.get(ChronoField.YEAR)); // 2000
System.out.println(year.get(ChronoField.YEAR_OF_ERA)); // 2000
System.out.println(year.get(ChronoField.ERA)); // 1
final var year = Year.of(-100);
System.out.println(year); // -100

System.out.println(year.get(ChronoField.YEAR)); // -100
System.out.println(year.get(ChronoField.YEAR_OF_ERA)); // 101
System.out.println(year.get(ChronoField.ERA)); // 0

long getLong (TemporalField field)

Gets the value of the specified field from this year as a long.

final var year = Year.of(2000);
System.out.println(year); // 2000

System.out.println(year.getLong(ChronoField.YEAR)); // 2000
System.out.println(year.getLong(ChronoField.YEAR_OF_ERA)); // 2000
System.out.println(year.getLong(ChronoField.ERA)); // 1
final var year = Year.of(-100);
System.out.println(year); // -100

System.out.println(year.getLong(ChronoField.YEAR)); // -100
System.out.println(year.getLong(ChronoField.YEAR_OF_ERA)); // 101
System.out.println(year.getLong(ChronoField.ERA)); // 0

int getValue ()

Gets the year value.

final var year = Year.of(2000);
System.out.println(year.getValue()); // 2000
final var year = Year.of(-100);
System.out.println(year.getValue()); // -100

int hashCode ()

A hash code for this year.

final var year = Year.of(2000);
System.out.println(year); // 2000
System.out.println(year.hashCode()); // 2000
final var year = Year.of(-100);
System.out.println(year); // -100
System.out.println(year.hashCode()); // -100

boolean isAfter (Year other)

Checks if this year is after the specified year.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var year2 = Year.of(1999);
final var year3 = Year.of(2000);
final var year4 = Year.of(2001);

System.out.println(year2); // 1999
System.out.println(year3); // 2000
System.out.println(year4); // 2001

System.out.println(year.isAfter(year2)); // true
System.out.println(year.isAfter(year3)); // false
System.out.println(year.isAfter(year4)); // false

boolean isBefore (Year other)

Checks if this year is before the specified year.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var year2 = Year.of(1999);
final var year3 = Year.of(2000);
final var year4 = Year.of(2001);

System.out.println(year2); // 1999
System.out.println(year3); // 2000
System.out.println(year4); // 2001

System.out.println(year.isBefore(year2)); // false
System.out.println(year.isBefore(year3)); // false
System.out.println(year.isBefore(year4)); // true

boolean isLeap ()

Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

final var year = Year.of(1999);
System.out.println(year); // 1999
System.out.println(year.isLeap()); // false

final var monthDay = MonthDay.of(2, 29);
System.out.println(monthDay); // --02-29
System.out.println(year.isValidMonthDay(monthDay)); // false
final var year = Year.of(2000);
System.out.println(year); // 2000
System.out.println(year.isLeap()); // true

final var monthDay = MonthDay.of(2, 29);
System.out.println(monthDay); // --02-29
System.out.println(year.isValidMonthDay(monthDay)); // true

static boolean isLeap (long year)

Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

System.out.println(Year.isLeap(1999)); // false

// DateTimeException: Invalid date 'February 29' as '1999' is not a leap year
//LocalDate.of(1999, 2, 29);
System.out.println(Year.isLeap(2000)); // true

final var date = LocalDate.of(2000, 2, 29);
System.out.println(date); // 2000-02-29

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var year = Year.of(2000);
System.out.println(year); // 2000

System.out.println(year.isSupported(ChronoField.YEAR)); // true
System.out.println(year.isSupported(ChronoField.YEAR_OF_ERA)); // true
System.out.println(year.isSupported(ChronoField.ERA)); // true

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

final var year = Year.of(2000);
System.out.println(year); // 2000

System.out.println(year.isSupported(ChronoUnit.YEARS)); // true
System.out.println(year.isSupported(ChronoUnit.DECADES)); // true
System.out.println(year.isSupported(ChronoUnit.CENTURIES)); // true
System.out.println(year.isSupported(ChronoUnit.MILLENNIA)); // true
System.out.println(year.isSupported(ChronoUnit.ERAS)); // true

boolean isValidMonthDay (MonthDay monthDay)

Checks if the month-day is valid for this year.

Please see isLeap().

int length ()

Gets the length of this year in days.

final var year1 = Year.of(1999);
System.out.println(year1); // 1999
System.out.println(year1.length()); // 365
System.out.println(year1.isLeap()); // false

final var year2 = Year.of(2000);
System.out.println(year2); // 2000
System.out.println(year2.length()); // 366
System.out.println(year2.isLeap()); //true

Year minus (long amountToSubtract, TemporalUnit unit)

Returns a copy of this year with the specified amount subtracted.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret1 = year.minus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 1999

final var ret2 = year.minus(2, ChronoUnit.YEARS);
System.out.println(ret2); // 1998

final var ret3 = year.minus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // -1000
final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret1 = year.minus(1, ChronoUnit.DECADES);
System.out.println(ret1); // 1990

final var ret2 = year.minus(1, ChronoUnit.CENTURIES);
System.out.println(ret2); // 1900

final var ret3 = year.minus(1, ChronoUnit.MILLENNIA);
System.out.println(ret3); // 1000

Year minus (TemporalAmount amountToSubtract)

Returns a copy of this year with the specified amount subtracted.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var period = Period.ofYears(20);
System.out.println(period); // P20Y

final var ret = year.minus(period);
System.out.println(ret); // 1980

Year minusYears (long yearsToSubtract)

Returns a copy of this Year with the specified number of years subtracted.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret1 = year.minusYears(1);
System.out.println(ret1); // 1999

final var ret2 = year.minusYears(2);
System.out.println(ret2); // 1998

final var ret3 = year.minusYears(3000);
System.out.println(ret3); // -1000

static Year now ()

Obtains the current year from the system clock in the default time-zone.

final var year = Year.now();
System.out.println(year); // 2022

static Year now (Clock clock)

Obtains the current year from the specified clock.

// Clock advanced for 10 years.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(3650));

System.out.println(Year.now()); // 2022
System.out.println(Year.now(clock)); // 2032

static Year now (ZoneId zone)

Obtains the current year from the system clock in the specified time-zone.

final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles

System.out.println(Year.now(zoneId)); // 2022

static Year of (int isoYear)

Obtains an instance of Year.

final var year1 = Year.of(2022);
System.out.println(year1); // 2022

final var year2 = Year.of(1999);
System.out.println(year2); // 1999

final var year3 = Year.of(-100);
System.out.println(year3); // -100

static Year parse (CharSequence text)

Obtains an instance of Year from a text string such as 2007.

final var year1 = Year.parse("2000");
System.out.println(year1); // 2000

final var year2 = Year.parse("1999");
System.out.println(year2); // 1999

final var year3 = Year.parse("-100");
System.out.println(year3); // -100

static Year parse (CharSequence text, DateTimeFormatter formatter)

Obtains an instance of Year from a text string using a specific formatter.

final var year1 = Year.parse("1999", DateTimeFormatter.ofPattern("yyyy"));
System.out.println(year1); // 1999

final var year2 = Year.parse("06", DateTimeFormatter.ofPattern("yy"));
System.out.println(year2); // 2006

Year plus (long amountToAdd, TemporalUnit unit)

Returns a copy of this year with the specified amount added.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret1 = year.plus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2001

final var ret2 = year.plus(2, ChronoUnit.YEARS);
System.out.println(ret2); // 2002

final var ret3 = year.plus(345, ChronoUnit.YEARS);
System.out.println(ret3); // 2345
final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret1 = year.plus(1, ChronoUnit.DECADES);
System.out.println(ret1); // 2010

final var ret2 = year.plus(1, ChronoUnit.CENTURIES);
System.out.println(ret2); // 2100

final var ret3 = year.plus(1, ChronoUnit.MILLENNIA);
System.out.println(ret3); // 3000

Year plus (TemporalAmount amountToAdd)

Returns a copy of this year with the specified amount added.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var period = Period.ofYears(20);
System.out.println(period); // P20Y

final var ret = year.plus(period);
System.out.println(ret); // 2020

Year plusYears (long yearsToAdd)

Returns a copy of this Year with the specified number of years added.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret1 = year.plusYears(1);
System.out.println(ret1); // 2001

final var ret2 = year.plusYears(2);
System.out.println(ret2); // 2002

final var ret3 = year.plusYears(345);
System.out.println(ret3); // 2345

<R> R query (TemporalQuery<R> query)

Queries this year using the specified query.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret = year.query(TemporalQueries.precision());
System.out.println(ret); // Years

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var year = Year.of(2000);
System.out.println(year); // 2000

System.out.println(year.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(year.range(ChronoField.YEAR_OF_ERA)); // 1 - 999999999
System.out.println(year.range(ChronoField.ERA)); // 0 - 1

String toString ()

Outputs this year as a String.

final var str1 = Year.of(1999).toString();
System.out.println(str1); // 1999

final var str2 = Year.of(2000).toString();
System.out.println(str2); // 2000

final var str3 = Year.of(-100).toString();
System.out.println(str3); // -100

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another year in terms of the specified unit.

final var start = Year.of(2000);
System.out.println(start); // 2000

final var end = Year.of(2050);
System.out.println(end); // 2050

final var ret1 = start.until(end, ChronoUnit.YEARS);
System.out.println(ret1); // 50

final var ret2 = start.until(end, ChronoUnit.DECADES);
System.out.println(ret2); // 5

Year with (TemporalAdjuster adjuster)

Returns an adjusted copy of this year.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret = year.with(temporal -> Year.of(temporal.get(ChronoField.YEAR) * 2));
System.out.println(ret); // 4000

Year with (TemporalField field, long newValue)

Returns a copy of this year with the specified field set to a new value.

final var year = Year.of(2000);
System.out.println(year); // 2000

final var ret = year.with(ChronoField.YEAR, 1999);
System.out.println(ret); // 1999

Related posts

To top of page