Java MonthDay getMonthValue()方法及示例

来源:这里教程网 时间:2026-02-17 20:59:02 作者:

Java MonthDay getMonthValue()方法及示例

Java中MonthDay类的 getMonthValue() 方法可以获得1到12的年份月份字段。

语法

public int getMonthValue()

参数。该方法不接受任何参数。

返回: 该函数返回年的月份,从1到12。

以下程序说明了 MonthDay.getMonthValue() 方法。

程序 1 :

// Program to illustrate the getMonthValue() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Parses the date        MonthDay tm1 = MonthDay.parse("--12-06");          // Uses the function        LocalDate dt1 = tm1.atYear(2018);          // Prints the date        System.out.println(dt1.getMonthValue());    }}

输出。

12

程序2

// Program to illustrate the getMonthValue() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Parses the date        MonthDay tm1 = MonthDay.parse("--01-09");          // Uses the function        LocalDate dt1 = tm1.atYear(2016);          // Prints the date        System.out.println(dt1.getMonthValue());    }}

输出。

1

参考资料: https://docs.oracle.com/javase/8/docs/api/java/time/MonthDay.html#getMonthValue-

相关推荐