java.time.Year represents a year in the ISO-8601 calendar system, such as 2021. It is an immutable date-time class that stores only the year, without a month, day, time, or time zone.
Yearsupports proleptic year numbering, in which year0exists between years-1and1.- It provides methods to create, retrieve, compare, modify, and format year values.
- It also provides methods to check whether a year is a leap year and to combine a year with a month or day to create date-related objects.
Example: The program creates a Year object for 2024 and checks whether it is a leap year.
import java.time.Year;
public class GFG {
public static void main(String[] args) {
Year year = Year.of(2024);
System.out.println("Year: " + year);
System.out.println("Leap year: " + year.isLeap());
System.out.println("Number of days: " + year.length());
}
}
Output
Year: 2024 Leap year: true Number of days: 366
Explanation: Year.of() creates the year, while isLeap() and length() provide information about it.
Syntax
public final class Year extends Object
implements Temporal, TemporalAdjuster,
Comparable<Year>, Serializable
Working with Year Methods
1. Using atDay()
The atDay() method combines a year with a specified day-of-year and returns a LocalDate.
Syntax
public LocalDate atDay(int dayOfYear)
import java.time.Year;
public class GFG {
public static void main(String[] args) {
Year year = Year.of(2024);
System.out.println(year.atDay(60));
}
}
Output
2024-02-29
Explanation: Day 60 of the leap year 2024 is February 29.
2. Using isAfter()
The isAfter() method checks whether one Year occurs after another Year.
Syntax
public boolean isAfter(Year otherYear)
import java.time.Year;
public class GFG {
public static void main(String[] args) {
Year year1 = Year.of(2025);
Year year2 = Year.of(2020);
System.out.println(year1.isAfter(year2));
}
}
Output
true
Explanation: The year 2025 occurs after the year 2020, so the method returns true.
3. Using isBefore()
The isBefore() method checks whether one Year occurs before another Year.
Syntax
public boolean isBefore(Year otherYear)
import java.time.Year;
public class GFG {
public static void main(String[] args) {
Year year1 = Year.of(2020);
Year year2 = Year.of(2025);
System.out.println(year1.isBefore(year2));
}
}
Output
true
Explanation: The year 2020 occurs before the year 2025, so the method returns true.
4. Using isLeap()
The isLeap() method checks whether the year is a leap year.
Syntax
public boolean isLeap()
import java.time.Year;
public class GFG {
public static void main(String[] args) {
Year year = Year.of(2024);
System.out.println(year.isLeap());
}
}
Output
true
Explanation: 2024 is divisible by 4 and is not a century year, so it is a leap year.
5. Using now()
The now() method obtains the current year from the system clock.
Syntax
public static Year now()
import java.time.Year;
public class GFG {
public static void main(String[] args) {
Year year = Year.now();
System.out.println("Current year: " + year);
}
}
Output
Current year: 2026
Explanation: Year.now() obtains the current year from the system clock.
Note: The output depends on the year in which the program is executed.
Commonly Used Methods
| Method | Description |
|---|---|
of() | Creates a Year object for the specified year. |
now() | Obtains the current year from the system clock. |
parse() | Creates a Year object by parsing a text value. |
atDay() | Combines the year with a day-of-year to create a LocalDate. |
atMonth() | Combines the year with a month to create a YearMonth. |
atMonthDay() | Combines the year with a month-day to create a LocalDate. |
isLeap() | Checks whether the year is a leap year. |
isAfter() | Checks whether this year occurs after another year. |
isBefore() | Checks whether this year occurs before another year. |
isValidMonthDay() | Checks whether a month-day is valid for this year. |
length() | Returns the number of days in the year. |
plusYears() | Adds the specified number of years. |
minusYears() | Subtracts the specified number of years. |
plus() | Returns a copy of the year with the specified amount added. |
minus() | Returns a copy of the year with the specified amount subtracted. |
getValue() | Returns the year as an integer. |
get() | Returns the value of a supported field as an int. |
getLong() | Returns the value of a supported field as a long. |
isSupported() | Checks whether a field or unit is supported. |
compareTo() | Compares this year with another year. |
equals() | Checks whether two Year objects are equal. |
hashCode() | Returns the hash code of the year. |
format() | Formats the year using a specified formatter. |
toString() | Returns the year as a string. |
query() | Queries this year using the specified query. |
range() | Returns the range of valid values for a field. |
until() | Calculates the amount of time until another year. |
with() | Returns an adjusted copy of the year. |
adjustInto() | Adjusts a temporal object to use this year. |