java.time.MonthDay Class in Java
Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. The java.time.MonthDay class represents a combination of month and day of the month, and it is immutable. java.time is a package used to work with the current date and time API. All the methods of this class are discussed below in the tabular format.
Method |
Description |
adjustInto(Temporal temporal) |
Adjusts the specified temporal object to having this month-day. |
atYear(int year) |
Combines this month-day with a year to create a LocalDate. |
compareTo(MonthDay other) |
Compares this month-day to another month-day. |
format(DateTimeFormatter formatter) |
Formats this month-day using the specified formatter. |
getDayOfMonth() |
Gets the day-of-month field. |
getMonth() |
Gets the month-of-year field using the Month enum. |
getMonthValue() |
Gets the month-of-year field from 1 to 12. |
hashCode() |
A hash code for this month-day. |
isAfter(MonthDay other) |
Checks if this month-day is after the specified month-day. |
now() |
Obtains the current month-day from the system clock in the default time-zone. |
now(Clock clock) |
Obtains the current month-day from the specified clock. |
of(int month, int dayOfMonth) |
Obtains an instance of MonthDay. |
query(TemporalQuery<R> query) |
Queries this month-day using the specified query. |
range(TemporalField field) |
Gets the range of valid values for the specified field. |
toString() |
Outputs this month-day as a String, such as –12-03. |
with(Month month) |
Returns a copy of this MonthDay with the month-of-year altered. |
withDayOfMonth(int dayOfMonth) |
Returns a copy of this MonthDay with the day-of-month altered. |
withMonth(int month) |
Returns a copy of this MonthDay with the month-of-year altered. |
Implementation: Let us now discuss a few of the methods of this class
- Import classes and package java.time.
- Now use method such as MonthDay.of() or any other method and store instance of MonthDay.
- Display the stored value in a variable.
Example 1
Java
import java.time.Month;
import java.time.MonthDay;
public class GFG {
public static void main(String[] args)
{
MonthDay monthday = MonthDay.of(Month.MARCH, 14 );
System.out.println(monthday);
}
}
|
Java
Java
import java.time.MonthDay;
public class GFG {
public static void main(String[] args)
{
MonthDay monthday = MonthDay.parse( "--03-14" );
System.out.println(monthday.getMonth());
}
}
|
Last Updated :
29 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...