Friday, December 27, 2024
Google search engine
HomeLanguagesJavajava.time.MonthDay Class in Java

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




// Java Program to illustrate MonthDay Class
  
// Importing Month and MonthDay classes
// from java.time package
import java.time.Month;
import java.time.MonthDay;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of MonthDay class by
        // storing instance of MonthDay by
        // passing date and month as arguments
  
        // Custom inputs are passed as arguments
        MonthDay monthday = MonthDay.of(Month.MARCH, 14);
  
        // Print and display the value stored
        System.out.println(monthday);
    }
}


Output

--03-14

Java

Java




// Java Program to illustrate MonthDay Class
  
// importing MonthDay class from java.time
import java.time.MonthDay;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Store an instance of MonthDay
        // from a text i.e --03-14
        MonthDay monthday = MonthDay.parse("--03-14");
  
        // Display the month using instance of class
        System.out.println(monthday.getMonth());
    }
}


Output

MARCH
RELATED ARTICLES

Most Popular

Recent Comments