The java.time.MonthDay class in Java represents a combination of a month and a day of the month, without a year or time. It is an immutable class provided by the java.time package, introduced in Java 8 as part of the modern Date and Time API. MonthDay is useful for representing recurring dates such as birthdays, anniversaries, and annual events, where the year is not required.
- MonthDay stores values such as --03-14 and does not store a year.
- It provides methods to create, compare, format, modify, and combine a month-day value with a year.
Example:
Input: Create a MonthDay object for March 14.
Output: --03-14
Syntax
public final class MonthDay
implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable
Approach
- Import the required classes from the
java.timepackage. - Create a
MonthDayobject using methods such asof()orparse(). - Use the appropriate methods to access or modify the month-day value.
1. Creating a MonthDay Object
MonthDay.of() method creates a month-day value using the Month enum and a day.
import java.time.Month;
import java.time.MonthDay;
public class GFG {
public static void main(String[] args) {
// Create a MonthDay object
MonthDay monthDay = MonthDay.of(Month.MARCH, 14);
// Display the month and day
System.out.println(monthDay);
}
}
Output
--03-14
Explanation: The MonthDay.of() method creates a MonthDay object representing March 14. Since MonthDay does not contain a year, the value is displayed in the --MM-dd format.
2: Parsing a MonthDay Value
The parse() method creates a MonthDay object from its text representation. The getMonth() method then returns the month as a Month enum value.
import java.time.MonthDay;
public class GFG {
public static void main(String[] args) {
// Create MonthDay from text
MonthDay monthDay = MonthDay.parse("--03-14");
// Display the month
System.out.println(monthDay.getMonth());
}
}
Output
MARCH
Explanation: The parse() method converts the text --03-14 into a MonthDay object. The getMonth() method returns MARCH, which is the corresponding value from the Month enum.
Methods Of MonthDay Class
| Method | Description |
|---|---|
adjustInto(Temporal temporal) | Adjusts the specified temporal object to have this month and day. |
atYear(int year) | Combines this month-day with a year to create a LocalDate. |
compareTo(MonthDay other) | Compares this month-day with another month-day. |
format(DateTimeFormatter formatter) | Formats this month-day using the specified formatter. |
getDayOfMonth() | Returns the day of the month. |
getMonth() | Returns the month using the Month enum. |
getMonthValue() | Returns the month as a value from 1 to 12. |
hashCode() | Returns the hash code of this month-day. |
isAfter(MonthDay other) | Checks whether this month-day occurs after another month-day. |
isBefore(MonthDay other) | Checks whether this month-day occurs before another month-day. |
isValidYear(int year) | Checks whether this month-day is valid for the specified year. |
now() | Obtains the current month-day from the system clock. |
now(Clock clock) | Obtains the current month-day from the specified clock. |
of(int month, int dayOfMonth) | Creates a MonthDay using the month and day values. |
of(Month month, int dayOfMonth) | Creates a MonthDay using a Month enum and day value. |
parse(CharSequence text) | Creates a MonthDay from a text representation. |
query(TemporalQuery<R> query) | Queries this month-day using the specified query. |
range(TemporalField field) | Returns the range of valid values for the specified field. |
toString() | Returns the month-day as a string, such as --12-03. |
with(Month month) | Returns a copy with the month changed. |
withDayOfMonth(int dayOfMonth) | Returns a copy with the day changed. |
withMonth(int month) | Returns a copy with the month changed. |