The YearMonth class in Java represents a combination of a year and a month, such as 2026-09. It is an immutable date-time class and does not store a day, time, or time zone.
- YearMonth belongs to the java.time package and implements Temporal, TemporalAdjuster, Comparable<YearMonth>, and Serializable.
- It provides methods to create, compare, modify, format, and retrieve information about a year and month.
Example: The program creates a YearMonth object for September 2026 and retrieves information about the month.
import java.time.YearMonth;
public class GFG {
public static void main(String[] args) {
YearMonth ym = YearMonth.of(2026, 9);
System.out.println("Year-Month: " + ym);
System.out.println("Month: " + ym.getMonth());
System.out.println("Days in month: " + ym.lengthOfMonth());
}
}
Output
Year-Month: 2026-09 Month: SEPTEMBER Days in month: 30
Explanation: YearMonth.of() creates the object, while getMonth() and lengthOfMonth() retrieve its month and number of days.
Syntax
public final class YearMonth
implements Temporal, TemporalAdjuster,
Comparable<YearMonth>, Serializable
Ways to Use YearMonth
1. Using plus()
The plus() method returns a new YearMonth with the specified amount added. The original object remains unchanged.
Syntax
public YearMonth plus(TemporalAmount amountToAdd)
import java.time.Period;
import java.time.YearMonth;
public class GFG {
public static void main(String[] args) {
YearMonth ym = YearMonth.of(2021, 2);
YearMonth result = ym.plus(Period.ofYears(3));
System.out.println("Original: " + ym);
System.out.println("After adding: " + result);
}
}
Output
Original: 2021-02 After adding: 2024-02
Explanation: plus() adds three years to the YearMonth and returns a new object.
2. Using minus()
The minus() method returns a new YearMonth with the specified amount subtracted.
Syntax
public YearMonth minus(TemporalAmount amountToSubtract)
import java.time.Period;
import java.time.YearMonth;
public class GFG {
public static void main(String[] args) {
YearMonth ym = YearMonth.of(2021, 2);
YearMonth result = ym.minus(Period.ofYears(3));
System.out.println("Original: " + ym);
System.out.println("After subtracting: " + result);
}
}
Output
Original: 2021-02 After subtracting: 2018-02
Explanation: minus() subtracts three years from the YearMonth and returns a new object.
Commonly Used Methods
| Method | Description |
|---|---|
of() | Creates a YearMonth from a year and month. |
now() | Obtains the current year and month. |
parse() | Creates a YearMonth from a text value. |
plusMonths() | Adds the specified number of months. |
plusYears() | Adds the specified number of years. |
minusMonths() | Subtracts the specified number of months. |
minusYears() | Subtracts the specified number of years. |
plus() | Returns a copy with the specified amount added. |
minus() | Returns a copy with the specified amount subtracted. |
atDay() | Combines the year-month with a day to create a LocalDate. |
atEndOfMonth() | Returns the last day of the month as a LocalDate. |
getYear() | Returns the year. |
getMonth() | Returns the month as a Month enum. |
getMonthValue() | Returns the month number from 1 to 12. |
isLeapYear() | Checks whether the year is a leap year. |
isValidDay() | Checks whether a day is valid for the year-month. |
lengthOfMonth() | Returns the number of days in the month. |
lengthOfYear() | Returns the number of days in the year. |
isAfter() | Checks whether this year-month is after another. |
isBefore() | Checks whether this year-month is before another. |
compareTo() | Compares two YearMonth objects. |
format() | Formats the year-month using a formatter. |
equals() | Checks whether two YearMonth objects are equal. |
hashCode() | Returns the hash code of the year-month. |
toString() | Returns the year-month as a string. |