Java OffsetDateTime getMonth()方法及示例

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

Java OffsetDateTime getMonth()方法及示例

Java中OffsetDateTime类的 getMonth() 方法使用Month枚举来获取年的月份字段。

语法:

public Month getMonth()

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

返回值 :它返回年份的月份,而不是空值。

以下程序说明了getMonth()方法。

程序1 :

// Java program to demonstrate the getMonth() method  import java.time.OffsetDateTime;  public class GFG {    public static void main(String[] args)    {          // parses a date        OffsetDateTime date = OffsetDateTime.parse("2018-12-03T12:30:30+01:00");          // Prints the month of given date        System.out.println("Month: " + date.getMonth());    }}

输出。

Month: DECEMBER

程序2 :

// Java program to demonstrate the getMonth() methodimport java.time.OffsetDateTime;  public class GFG {    public static void main(String[] args)    {          // parses a date        OffsetDateTime date = OffsetDateTime.parse("2016-11-31T12:30:30+05:00");          // Prints the month of given date        System.out.println("Month: " + date.getMonth());    }}

输出。

Month: NOVEMBER

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

相关推荐