Introduction
MonthDay
in Java, part of the java.time
package, represents a combination of a month and a day without a year. It is useful for handling recurring dates like birthdays or anniversaries.
Table of Contents
- What is
MonthDay
? - Creating
MonthDay
Instances - Common Methods
- Examples of
MonthDay
- Conclusion
1. What is MonthDay?
MonthDay
is an immutable class that combines a month and a day. It does not store or represent a year, making it ideal for recurring events that occur annually.
2. Creating MonthDay Instances
You can create MonthDay
instances in several ways:
MonthDay.now()
: Obtains the current month and day from the system clock.MonthDay.of(Month month, int dayOfMonth)
: Creates an instance with the specified month and day.MonthDay.parse(CharSequence text)
: Parses a string to aMonthDay
using the ISO-8601 format.
3. Common Methods
getMonth()
: Returns the month part of thisMonthDay
.getDayOfMonth()
: Returns the day of the month.isValidYear(int year)
: Checks if thisMonthDay
is valid for the specified year.withMonth(int month)
: Returns a copy of thisMonthDay
with the specified month.withDayOfMonth(int dayOfMonth)
: Returns a copy of thisMonthDay
with the specified day.
4. Examples of MonthDay
Example 1: Getting the Current Month and Day
This example demonstrates how to get the current month and day using MonthDay.now()
.
import java.time.MonthDay;
public class CurrentMonthDayExample {
public static void main(String[] args) {
MonthDay today = MonthDay.now();
System.out.println("Today's Month and Day: " + today);
}
}
Output:
Today's Month and Day: --06-30
Example 2: Creating a Specific Month and Day
Here, we create a specific MonthDay
using MonthDay.of(Month month, int dayOfMonth)
.
import java.time.Month;
import java.time.MonthDay;
public class SpecificMonthDayExample {
public static void main(String[] args) {
MonthDay birthday = MonthDay.of(Month.JUNE, 15);
System.out.println("Specific Month and Day: " + birthday);
}
}
Output:
Specific Month and Day: --06-15
Example 3: Parsing a MonthDay String
This example shows how to parse a string into a MonthDay
using MonthDay.parse(CharSequence text)
.
import java.time.MonthDay;
public class ParseMonthDayExample {
public static void main(String[] args) {
MonthDay anniversary = MonthDay.parse("--06-15");
System.out.println("Parsed Month and Day: " + anniversary);
}
}
Output:
Parsed Month and Day: --06-15
Example 4: Checking Validity for a Year
In this example, we demonstrate how to check if a MonthDay
is valid for a given year using isValidYear(int year)
.
import java.time.MonthDay;
public class ValidityCheckExample {
public static void main(String[] args) {
MonthDay date = MonthDay.of(2, 29);
boolean isValidIn2023 = date.isValidYear(2023);
boolean isValidIn2024 = date.isValidYear(2024);
System.out.println("Is valid in 2023: " + isValidIn2023);
System.out.println("Is valid in 2024: " + isValidIn2024);
}
}
Output:
Is valid in 2023: false
Is valid in 2024: true
Example 5: Modifying Month and Day
This example shows how to modify the month and day of a MonthDay
using withMonth(int month)
and withDayOfMonth(int dayOfMonth)
.
import java.time.MonthDay;
public class ModifyMonthDayExample {
public static void main(String[] args) {
MonthDay original = MonthDay.of(5, 10);
MonthDay modifiedMonth = original.withMonth(8);
MonthDay modifiedDay = original.withDayOfMonth(20);
System.out.println("Original: " + original);
System.out.println("Modified Month: " + modifiedMonth);
System.out.println("Modified Day: " + modifiedDay);
}
}
Output:
Original: --05-10
Modified Month: --08-10
Modified Day: --05-20
Conclusion
The MonthDay
class in Java is used for handling dates that recur annually, such as birthdays or anniversaries. It provides methods to manipulate and retrieve information about months and days without the need for a year component. Using MonthDay
can simplify date management in applications dealing with recurring events.
Comments
Post a Comment
Leave Comment