Java month getValue()方法

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

Java month getValue()方法

getValue() 方法是Month ENUM的一个内置方法,用于从这个Month实例中获取整数的年月日值。

该方法返回的值在1-12的范围内,代表从一月到十二月的月份。

语法:

public int getValue()

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

返回值 :该方法以整数形式从这个Month实例中返回年月的值。

下面的程序说明了上述方法。

程序1 :

import java.time.*;import java.time.Month;import java.time.temporal.ChronoField;  class monthEnum {    public static void main(String[] args)    {        // Create a month instance        Month month = Month.MARCH;          // Get the value of month-of-year as int        System.out.println(month.getValue());    }}

输出。

3

程序2 :

import java.time.*;import java.time.Month;import java.time.temporal.ChronoField;  class monthEnum {    public static void main(String[] args)    {        // Create a month instance        Month month = Month.DECEMBER;          // Get the value of month-of-year as int        System.out.println(month.getValue());    }}

输出。

12

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

相关推荐