Java OffsetDateTime getMonthValue()方法及示例

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

Java OffsetDateTime getMonthValue()方法及示例

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

语法:

public int getMonthValue()

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

返回值 :它返回从1到12的年份的月份。

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

程序1 :

// Java program to demonstrate the getMonthValue() 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.getMonthValue());    }}

程序2 :

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

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

相关推荐