Java : Year with Examples

Year (Java SE 21 & JDK 21) 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(2100);
System.out.println(year.getValue()); // 2100
System.out.println(year.length()); // 365

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(1800);
System.out.println(year); // 1800

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

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

LocalDate atDay (int dayOfYear)

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

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

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

YearMonth atMonth (int month)

Combines this year with a month to create a YearMonth.

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

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

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

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

YearMonth atMonth (Month month)

Combines this year with a month to create a YearMonth.

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

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

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

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

LocalDate atMonthDay (MonthDay monthDay)

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

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

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

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

int compareTo (Year other)

Compares this year to another year.

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

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

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(2100);
final var year2 = Year.of(2100);

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

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(2100);
System.out.println(year); // 2100

final var ret = year.format(DateTimeFormatter.ofPattern("yyyy"));
System.out.println(ret); // 2100
final var year = Year.of(2099);
System.out.println(year); // 2099

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

static Year from (TemporalAccessor temporal)

Obtains an instance of Year from a temporal object.

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

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

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

int get (TemporalField field)

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

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

System.out.println(year.get(ChronoField.YEAR)); // 2100
System.out.println(year.get(ChronoField.YEAR_OF_ERA)); // 2100
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(2100);
System.out.println(year); // 2100

System.out.println(year.getLong(ChronoField.YEAR)); // 2100
System.out.println(year.getLong(ChronoField.YEAR_OF_ERA)); // 2100
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(2100);
System.out.println(year.getValue()); // 2100
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(2100);
System.out.println(year); // 2100
System.out.println(year.hashCode()); // 2100
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(2100);
System.out.println(year); // 2100

final var year2 = Year.of(2099);
final var year3 = Year.of(2100);
final var year4 = Year.of(2101);

System.out.println(year2); // 2099
System.out.println(year3); // 2100
System.out.println(year4); // 2101

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(2100);
System.out.println(year); // 2100

final var year2 = Year.of(2099);
final var year3 = Year.of(2100);
final var year4 = Year.of(2101);

System.out.println(year2); // 2099
System.out.println(year3); // 2100
System.out.println(year4); // 2101

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(2103);
System.out.println(year); // 2103
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(2104);
System.out.println(year); // 2104
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(2103)); // false
System.out.println(Year.isLeap(2104)); // true
System.out.println(Year.isLeap(2105)); // false
System.out.println(Year.isLeap(2106)); // false
System.out.println(Year.isLeap(2107)); // false
System.out.println(Year.isLeap(2108)); // true

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

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

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(2100);
System.out.println(year); // 2100

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.

final var year = Year.of(2103);
System.out.println(year); // 2103
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(2104);
System.out.println(year); // 2104
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

int length ()

Gets the length of this year in days.

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

final var year2 = Year.of(2104);
System.out.println(year2); // 2104
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(2100);
System.out.println(year); // 2100

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

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

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

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

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

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

Year minus (TemporalAmount amountToSubtract)

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

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

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

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

Year minusYears (long yearsToSubtract)

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

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

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

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

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

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); // 2024

static Year now (Clock clock)

Obtains the current year from the specified clock.

// A clock advanced 1 year.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(365));

System.out.println(Year.now()); // 2024
System.out.println(Year.now(clock)); // 2025

static Year now (ZoneId zone)

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

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

System.out.println(Year.now(zone)); // 2024

static Year of (int isoYear)

Obtains an instance of Year.

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

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

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("2100");
System.out.println(year1); // 2100

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

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("2100", DateTimeFormatter.ofPattern("yyyy"));
System.out.println(year1); // 2100

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

Year plus (long amountToAdd, TemporalUnit unit)

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

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

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

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

final var ret3 = year.plus(7900, ChronoUnit.YEARS);
System.out.println(ret3); // 10000
final var year = Year.of(2100);
System.out.println(year); // 2100

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

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

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

Year plus (TemporalAmount amountToAdd)

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

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

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

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

Year plusYears (long yearsToAdd)

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

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

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

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

final var ret3 = year.plusYears(7900);
System.out.println(ret3); // 10000

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

Queries this year using the specified query.

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

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(2100);
System.out.println(year); // 2100

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(1800).toString();
System.out.println(str1); // 1800

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

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(2100);
System.out.println(start); // 2100

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

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(2100);
System.out.println(year); // 2100

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

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(2100);
System.out.println(year); // 2100

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

Related posts

To top of page