The Java MonthDay class is a part of the Java 8 Date/Time API that represents a combination of month and day-of-month, without a year. It is a time-agnostic class that represents a recurring date in a specific month, such as the 15th of every month.
The MonthDay class is immutable, which means that once a MonthDay object is created, its state cannot be changed. It has two instance variables – month and day – which can be accessed using the getMonth()
and getDayOfMonth()
methods respectively.
Here is an example of creating a MonthDay object and accessing its values:
MonthDay monthDay = MonthDay.of(Month.JULY, 15); System.out.println("Month: " + monthDay.getMonth()); // output: Month: JULY System.out.println("Day of Month: " + monthDay.getDayOfMonth()); // output: Day of Month: 15
The MonthDay class provides various methods to manipulate and compare MonthDay objects. Some of the useful methods are withMonth()
, withDayOfMonth()
, plus()
, minus()
, isBefore()
, isAfter()
, and equals()
.
Here is an example of using some of these methods:
MonthDay monthDay = MonthDay.of(Month.JULY, 15); MonthDay updatedMonthDay = monthDay.withMonth(Month.AUGUST.getValue()).withDayOfMonth(10); System.out.println("Updated MonthDay: " + updatedMonthDay); // output: Updated MonthDay: --08-10 System.out.println("Is updatedMonthDay before monthDay? " + updatedMonthDay.isBefore(monthDay)); // output: Is updatedMonthDay before monthDay? false System.out.println("Is updatedMonthDay after monthDay? " + updatedMonthDay.isAfter(monthDay)); // output: Is updatedMonthDay after monthDay? true
Overall, the MonthDay class is a useful class for representing recurring events that occur on a particular day of a particular month, and it provides various methods to manipulate and compare MonthDay objects.
JJava MonthDay Class Declaration:
The Java MonthDay class is a part of the Java 8 Date/Time API that represents a combination of month and day-of-month, without a year. It is a final class, which means that it cannot be extended, and it has a private constructor, which means that instances of this class can only be created using the factory methods provided by the class itself.
The MonthDay class declaration is as follows:
public final class MonthDay implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable
As you can see, the MonthDay class implements the TemporalAccessor, TemporalAdjuster, Comparable, and Serializable interfaces, which provide various methods for accessing, adjusting, comparing, and serializing MonthDay objects.
The class has two private final fields – month
and day
– which represent the month and day-of-month of a MonthDay object. These fields are initialized through the private constructor that takes these values as arguments.
The MonthDay class provides several factory methods to create instances of MonthDay objects, such as of(int month, int dayOfMonth)
, of(Month month, int dayOfMonth)
, and now()
. It also provides various instance methods to manipulate and compare MonthDay objects, such as withMonth(int month)
and withDayOfMonth(int dayOfMonth)
, plus(long amount, TemporalUnit unit)
and minus(long amount, TemporalUnit unit)
, isBefore(MonthDay other)
and isAfter(MonthDay other)
, and equals(Object obj)
.
Overall, the MonthDay class provides a convenient way to represent a combination of month and day-of-month without a year, and it offers a range of methods to manipulate and compare MonthDay objects.
Methods of Java MonthDay Class:
The Java MonthDay class provides several methods to manipulate and compare MonthDay objects. Here are some of the commonly used methods:
of(int month, int dayOfMonth)
– creates a MonthDay object with the specified month and day-of-month.Example:
MonthDay monthDay = MonthDay.of(7, 15);
of(Month month, int dayOfMonth)
– creates a MonthDay object with the specified month and day-of-month.Example:
MonthDay monthDay = MonthDay.of(Month.JULY, 15);
from(TemporalAccessor temporal)
– obtains an instance of MonthDay from a temporal object.Example:
MonthDay monthDay = MonthDay.from(LocalDateTime.now());
getMonthValue()
– returns the value of the month field as an int.Example:
int month = monthDay.getMonthValue();
getMonth()
– returns the month field as a Month object.Example:
Month month = monthDay.getMonth();
getDayOfMonth()
– returns the value of the day-of-month field as an int.Example:
int dayOfMonth = monthDay.getDayOfMonth();
withMonth(int month)
– returns a copy of the MonthDay with the month field set to the specified value.Example:
MonthDay updatedMonthDay = monthDay.withMonth(8);
withDayOfMonth(int dayOfMonth)
– returns a copy of the MonthDay with the day-of-month field set to the specified value.Example:
MonthDay updatedMonthDay = monthDay.withDayOfMonth(10);
plus(long amount, TemporalUnit unit)
– returns a copy of the MonthDay with the specified amount added to the month or day-of-month.Example:
MonthDay updatedMonthDay = monthDay.plus(2, ChronoUnit.MONTHS);
minus(long amount, TemporalUnit unit)
– returns a copy of the MonthDay with the specified amount subtracted from the month or day-of-month.
Example: MonthDay updatedMonthDay = monthDay.minus(5, ChronoUnit.DAYS);
isBefore(MonthDay other)
– checks if this MonthDay is before the specified MonthDay.Example:
boolean isBefore = monthDay.isBefore(updatedMonthDay);
isAfter(MonthDay other)
– checks if this MonthDay is after the specified MonthDay.Example:
boolean isAfter = monthDay.isAfter(updatedMonthDay);
equals(Object obj)
– checks if this MonthDay is equal to the specified object.Example:
boolean isEqual = monthDay.equals(updatedMonthDay);
Overall, the MonthDay class provides a range of methods to manipulate and compare MonthDay objects, making it a useful class for representing recurring events that occur on a particular day of a particular month.
Java MonthDay Class Example:
Here’s an example that demonstrates the usage of the Java MonthDay class:
import java.time.Month; import java.time.MonthDay; public class MonthDayExample { public static void main(String[] args) { MonthDay monthDay = MonthDay.of(Month.MARCH, 20); System.out.println("Month day: " + monthDay); // prints "Month day: --03-20" MonthDay updatedMonthDay = monthDay.withMonth(5); System.out.println("Updated month day: " + updatedMonthDay); // prints "Updated month day: --05-20" MonthDay anotherMonthDay = MonthDay.of(Month.APRIL, 15); boolean isBefore = monthDay.isBefore(anotherMonthDay); System.out.println("Is month day before another month day? " + isBefore); // prints "Is month day before another month day? true" boolean isEqual = monthDay.equals(anotherMonthDay); System.out.println("Is month day equal to another month day? " + isEqual); // prints "Is month day equal to another month day? false" } }
In this example, we first create a MonthDay object with the month of March and the day-of-month 20. We then print the value of this MonthDay object to the console using the toString()
method.
Next, we create a new MonthDay object with the month of May and the same day-of-month as the original MonthDay object, by using the withMonth()
method. We print the updated MonthDay object to the console.
We then create another MonthDay object with the month of April and the day-of-month 15. We use the isBefore()
method to check if the original MonthDay object is before this new MonthDay object, and print the result to the console.
Finally, we use the equals()
method to check if the original MonthDay object is equal to the new MonthDay object that we created earlier, and print the result to the console.
Java MonthDay Class Example: isValidYear()
The Java MonthDay class does not have a isValidYear()
method. However, you can use the atYear()
method of the LocalDate
class to create a LocalDate
object with the specified year, and then check if the resulting date is valid or not. Here’s an example:
import java.time.LocalDate; import java.time.Month; import java.time.MonthDay; public class MonthDayExample { public static void main(String[] args) { MonthDay monthDay = MonthDay.of(Month.FEBRUARY, 29); System.out.println("Month day: " + monthDay); // prints "Month day: --02-29" boolean isValid = false; for (int year = 1900; year <= 2100; year++) { LocalDate date = monthDay.atYear(year); if (date != null) { isValid = true; break; } } System.out.println("Is February 29th a valid date? " + isValid); // prints "Is February 29th a valid date? true" } }
In this example, we create a MonthDay
object with the month of February and the day-of-month 29, which is an invalid date for most years. We then loop through a range of years from 1900 to 2100, and use the atYear()
method to create a LocalDate
object with the specified year and the same month and day-of-month as the original MonthDay
object. If the resulting LocalDate
object is not null, it means that the date is valid for that year, and we set the isValid
flag to true and break out of the loop.
Finally, we print the value of the isValid
flag to the console, which tells us whether February 29th is a valid date or not. In this case, the output will be “Is February 29th a valid date? true”, because February 29th is a valid date in leap years.
Java MonthDay Class Example: get()
The get()
method is not available in the MonthDay
class. However, you can use the getMonthValue()
and getDayOfMonth()
methods to retrieve the values of the month and day-of-month, respectively. Here’s an example:
import java.time.Month; import java.time.MonthDay; public class MonthDayExample { public static void main(String[] args) { MonthDay monthDay = MonthDay.of(Month.AUGUST, 15); int monthValue = monthDay.getMonthValue(); int dayOfMonth = monthDay.getDayOfMonth(); System.out.println("Month value: " + monthValue); // prints "Month value: 8" System.out.println("Day of month: " + dayOfMonth); // prints "Day of month: 15" } }
In this example, we create a MonthDay
object with the month of August and the day-of-month 15. We then use the getMonthValue()
method to retrieve the value of the month, which is 8 for August (as it is the eighth month of the year). We also use the getDayOfMonth()
method to retrieve the value of the day-of-month, which is 15. Finally, we print the values of these variables to the console using the println()
method.
Java MonthDay Class Example: range()
The range()
method in the MonthDay
class returns the range of valid values for a specific ChronoField
. In the case of MonthDay
, it only has two ChronoField
s: MONTH_OF_YEAR
and DAY_OF_MONTH
.
Here’s an example that demonstrates the usage of the range()
method to get the range of valid values for the MONTH_OF_YEAR
and DAY_OF_MONTH
fields of a MonthDay
object:
import java.time.Month; import java.time.MonthDay; import java.time.temporal.ChronoField; public class MonthDayExample { public static void main(String[] args) { MonthDay monthDay = MonthDay.of(Month.JANUARY, 1); System.out.println("Month day: " + monthDay); // Get the range of valid values for MONTH_OF_YEAR field int minMonthValue = monthDay.range(ChronoField.MONTH_OF_YEAR).getMinimum(); int maxMonthValue = monthDay.range(ChronoField.MONTH_OF_YEAR).getMaximum(); System.out.println("Minimum month value: " + minMonthValue); // prints "Minimum month value: 1" System.out.println("Maximum month value: " + maxMonthValue); // prints "Maximum month value: 12" // Get the range of valid values for DAY_OF_MONTH field int minDayOfMonth = monthDay.range(ChronoField.DAY_OF_MONTH).getMinimum(); int maxDayOfMonth = monthDay.range(ChronoField.DAY_OF_MONTH).getMaximum(); System.out.println("Minimum day of month: " + minDayOfMonth); // prints "Minimum day of month: 1" System.out.println("Maximum day of month: " + maxDayOfMonth); // prints "Maximum day of month: 31" } }
In this example, we create a MonthDay
object with the month of January and the day-of-month 1. We then use the range()
method to get the range of valid values for the MONTH_OF_YEAR
field and the DAY_OF_MONTH
field.
We first get the minimum and maximum valid values for the MONTH_OF_YEAR
field by calling the range()
method with the ChronoField.MONTH_OF_YEAR
argument. We store the minimum and maximum values in the minMonthValue
and maxMonthValue
variables, respectively, and print them to the console using the println()
method.
We then get the minimum and maximum valid values for the DAY_OF_MONTH
field in a similar way by calling the range()
method with the ChronoField.DAY_OF_MONTH
argument. We store the minimum and maximum values in the minDayOfMonth
and maxDayOfMonth
variables, respectively, and print them to the console.
The output of the program will be:
Month day: --01-01 Minimum month value: 1 Maximum month value: 12 Minimum day of month: 1 Maximum day of month: 31