使用getMonthValue()Java中LocalDate类中的方法获得一年中的月份。此方法不需要任何参数,它返回一年中的月份,范围在1到12之间。
演示此的程序如下所示-
import java.time.*;
public class Main {
public static void main(String[] args) {
LocalDate ld = LocalDate.parse("2019-02-14");
System.out.println("The LocalDate is: " + ld);
System.out.println("The month is: " + ld.getMonthValue());
}
}输出结果
The LocalDate is: 2019-02-14 The month is: 2
现在让我们了解上面的程序。
首先显示LocalDate。然后使用getMonthValue()方法获得一年中的月份并显示出来。演示这的代码片段如下-
LocalDate ld = LocalDate.parse("2019-02-14");
System.out.println("The LocalDate is: " + ld);
System.out.println("The month is: " + ld.getMonthValue());