An instance of Year class represents the year ISO-8601 calendar system, such as 2022. Any field that can be derived from a year, can be obtained using this class. This class doesn’t store day, month, time or timezone information, for example value “2022” can be stored in Year, however the value “2nd October 2022” cannot be stored in Year.
Year class:
public final class Year extends Object implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable
Java Year class – Method Summary
Method | Description |
---|---|
LocalDate atDay(int day) | It combines this year with a day-of-year to create an instance of LocalDate. |
boolean isLeap() | Checks whether this Year is a Leap year or not. Returns true or false based on the result. |
int length() | This method returns the length of the year in number of days. |
static Year now() | Fetches the current year from system in default time zone. |
boolean isAfter(Year otherYear) | It checks if this year is after the specified year. |
boolean isBefore(Year otherYear) | It checks if this year is before the specified year. |
String format(DateTimeFormatter formatter) | Formats the Year using the specified formatter. |
int get(TemporalField field) | Returns the value of the specified field from this year. |
boolean equals(Object obj) | Checks if this year is equal to other specified year. |
int compareTo(Year other) | Compares this year to another Year and return int value. The difference between equals() and this method is that the equals() method returns true or false. However this method 0 if the years are equal, negative if this year is less than specified year and positive value if this year is greater than specified year. |
YearMonth atMonth(Month month) | Combines this year with the specified month to create YearMonth |
Java Year – now() and length() methods example
Method now()
is used to get the current year from the system in default time-zone. Method length()
returns the number of days in this year.
import java.time.Year; public class JavaExample { public static void main(String[] args) { Year year = Year.now(); System.out.println("Current Year: "+year); int length = year.length(); System.out.println("Length of the year: "+length); } }
Output:
Current Year: 2022 Length of the year: 365
Java Year -isLeap() method example
In the following example, we are using isLeap()
method to check if the specified year is leap year or not. This method return true if the year is leap year else it returns false. As you see in this example, this method returned false for year “2022
” and returned true for year “2020
“.
import java.time.Year; public class JavaExample { public static void main(String[] args) { Year year = Year.of(2022); System.out.println(year.isLeap()); Year year2 = Year.of(2020); System.out.println(year2.isLeap()); } }
Output:
false true
Java Year – atDay() & atMonth() example
We can add day of the year or month to this Year
so that we can get Local date or an instance of YearMonth. Here atDay()
method is used to combine the number of days to the year so that we can get an instance of LocalDate
.
Method atMonth()
combine number of months to this year to return YearMonth.
import java.time.LocalDate; import java.time.Year; import java.time.YearMonth; public class JavaExample{ public static void main(String[] args) { Year year = Year.of(2022); LocalDate localDate = year.atDay(150); System.out.println(localDate); YearMonth yearMonth = year.atMonth(4); System.out.println(yearMonth); } }
Output:
2022-05-30 2022-04
Reference:
Leave a Reply