Java : Year with Examples
Year (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Year methods.
Summary
final var year = Year.of(2100);
System.out.println(year.getValue()); // 2100
System.out.println(year.length()); // 365
Fields
static final int MAX_VALUE
System.out.println(Year.MAX_VALUE); // 999999999
static final int MIN_VALUE
System.out.println(Year.MIN_VALUE); // -999999999
Methods
Temporal adjustInto (Temporal temporal)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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 ()
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)
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)
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 ()
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)
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)
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)
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)
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 ()
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)
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)
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)
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 ()
final var year = Year.now();
System.out.println(year); // 2024
static Year now (Clock 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)
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
System.out.println(Year.now(zone)); // 2024
static Year of (int isoYear)
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)
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)
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)
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)
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)
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)
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)
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 ()
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)
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)
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)
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
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset