java.time.Year Class in Java
The java.time.Year class represents a year in the ISO-8601 calendar system, such as 2021. Year is an immutable date-time object that represents a year. This class does not store or represent a month, day, time, or time-zone. The years represented by this class follow the proleptic numbering system that is as follows:
The year 0 is preceded by year -1 and succeeded by the year 1.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Syntax: Class Declaration
public final class Year extends Object implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable
Now let us do discuss all the methods of this Year class which is shown below in a tabular format. Later on, we will be discussing them individually.
|adjustsIntro()||Adjusts the specified temporal object to have this year.|
|atDay()||Creates a local date with day number equal to the number in the Year class object|
|atMonth()||Combine this year with a month to create a YearMonth|
|atMonthDay()||Combine this year with a month date to create a LocalDate|
|compareTo()||Compares the two-year objects.|
|equals()||Checks if two-year objects are equal.|
|format()||Formats the year using specified datetime formatter.|
|get()||Gets the value of the specified field from this year.|
|getLong()||Getting the value of a specified field from this year as a long|
|getValue()||Returns the integer value of the year.|
|hashCode()||Returns the hashcode of the year object.|
|isAfter()||Checks whether the year is after year1.|
|isBefore()||Checks whether the year is before year1.|
|isLeap()||Checks whether the year is a leap year or not.|
|isSupported()||Checks if the specified temporal field is supported.|
|isValidMonthDay()||Checks if the month-day is valid for this year|
|length()||Returns the number of days in the year.|
|minus()||Returns the copy of this year with the specified amount subtracted|
|minusYears()||Subtracts a number of years from a year and returns a new Year object.|
|now()||Gets current year and creates a Year object.|
“Creates Year object with year equal to the number.
0 is used for 0 AD whereas -1 is used for 1 BC.”
|parse()||Obtains an instance of Year from a text string such as 2021.|
|plus()||Returns a copy of this year with the specified number of years added.|
|plusYears()||Adds num years to a year and returns a new Year object.|
|query()||Queries this year using the specified query.|
|range()||getting the range of valid values for the specified field.|
|toString()||Returns the current year as a string.|
|until()||Calculates the amount of time until another year in terms of the specified unit.|
|with()||Returns an adjusted copy of the year.|
Let us discuss a few of the frequently used methods of this class in detail to get a better understanding so as to implement the rest of them too. Here we will be discussing our major methods namely as follows:
Method 1: atDay() method of Year class in Java combines the current year with a day-of-year passed as a parameter to it to create a LocalDate.
public LocalDate atDay(int dayOfYear)
Parameter: This method accepts a single parameter day of the year. It is the day-of-year to use. It can take values from 1 to 365-366.
Return Value: It returns a Local Date formed by the current year and the date of the year passed as a parameter to the function.
Exception: This method throws a DateTimeException, if the day of year passed in the parameter is not valid, that is zero or less, 366 or greater or equal to 366 and the current year is not a leap year.
Method 2: hashCode()
It returns the hashcode value as an Integer. Hashcode value is mostly used in hashing–based collections like HashMap, HashSet, HashTable,etc. This method must be overridden in every class which overrides the equals() method.
public int hashCode() // This method returns the hash code value // for the object on which this method is invoked.
The isAfter() method of Year class in Java is used to check if this current Year object is after the Year specified as a parameter to this method.
public boolean isAfter(Year otherYear)
Parameter: It accepts a single parameter another year with which the current Year object is to be compared.
Return Value: It returns a boolean True value if this Year object’s value is after the value of Year object specified as a parameter to the method, otherwise it returns False.
The isBefore() method of Year class in Java is used to check if this current Year object is before the Year specified as parameter to this method.
public boolean isBefore(Year otherYear)
Parameter: It accepts a single parameter otherYear with which the current Year object is to be compared.
Return Value: It returns a boolean True value if this Year object’s value is before the value of Year object specified as a parameter to the method, otherwise it returns False.
The isLeap() method of Year class in Java is used to check if this Year object is a leap year or not according to the proleptic calendar system rules.
A year is a leap year if it has 366 days in it. According to the proleptic calendar system rules, a year is a Leap year if:
- If it is divisible by 4.
- It is not divisible by 100, but it can be divisible by 400.
public boolean isLeap()
Parameter: This method does not accept any parameter.
Return Value: It returns a boolean True value if this Year object’s value is a leap year according to the proleptic calendar system rules, otherwise it returns False.
The now() method of Year class in Java is used to return the current year from the system clock in the default time-zone.
public static Year now() or, public static Year now(Clock clock) or, public static Year now(ZoneId zone)
Parameter: The parameter is optional for this method as shown in the above syntax.
Return Value: It returns the current year from the system clock in the default time-zone if no parameter is specified otherwise it uses the specified clock and time zone to return the current year. It can never return a NULL value
The minus() method of Year class is used to return a copy of this year after subtracting the specified amount of unit from this Year object.An exception is thrown, If the specified unit cannot be subtracted from Year. This instance is immutable and unaffected by this method call.
public Year minus(long amountToSubtract, TemporalUnit unit)
Parameters: This method accepts two parameters which are s follow:
- This parameter represents the amount of the unit to subtract from the result.
- This parameter represents the unit of the amount to subtract.
Return Value: This method returns a Year based on this year with the specified amount subtracted.
Exception: This method throws the following Exceptions:
- This exception is thrown if the subtraction cannot be made.
- This exception is thrown if the unit is not supported.
- This exception is thrown if numeric overflow occurs.
You can co-relate the same for the plus() method. By now we are over with discussing the methods. Now it’s time to implement the same to demonstrate the Year class.
Current year: 2021 Local date: 2021-12-16 Year and month: 2021-06 Number of days: 365 2021 is before 2022: true 2021 is after 2020: true Hashcode of the year: 2021 2020 is leap year: true 2021 - 155 years: 1866 2021 + 145 years: 2166