Java MonthDay getMonth()方法及示例

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

Java MonthDay getMonth()方法及示例

Java中 MonthDay类getMonth() 方法从一个时间对象中获取MonthDay的一个实例。

语法

public Month getMonth()

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

返回: 该函数返回年月日,且不为空。

下面的程序说明了 MonthDay.getMonth() 方法。

程序1 :

// Program to illustrate the getMonth() 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.getMonth());    }}

输出

DECEMBER

程序2

// Program to illustrate the getMonth() 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.getMonth());    }}

输出

JANUARY

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

相关推荐