📜  Java的.time.MonthDay类在Java中

📅  最后修改于: 2022-05-13 01:55:20.341000             🧑  作者: Mango

Java的.time.MonthDay类在Java中

Java是最流行的编程语言和广泛使用的编程语言。 Java用于各种应用程序,如移动应用程序、桌面应用程序、Web 应用程序。 Java.time.MonthDay 类表示月份和月份中的某一天的组合,它是不可变的。 Java.time 是一个用于处理当前日期和时间 API 的包。下面以表格形式讨论了此类的所有方法。

MethodDescription
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 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.

实现:现在让我们讨论一下这个类的几个方法

  • 导入类并打包Java.time。
  • 现在使用诸如 MonthDay.of() 之类的方法或任何其他方法并存储 MonthDay 的实例。
  • 显示变量中存储的值。

示例 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);
    }
}


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());
    }
}


输出
--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());
    }
}
输出
MARCH