LocalDateTime 类中的 getMonthValue() 方法在 Java 中


在 Java 中,LocalDateTime 类中的 getMonthValue() 方法用于获取一年中的月份。此方法不需要参数,它返回的一年中的月份介于 1 到 12 之间。

一个演示此方法的程序如下 -

示例

 在线演示

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      LocalDateTime ldt = LocalDateTime.parse("2019-02-18T23:15:30");
      System.out.println("The LocalDateTime is: " + ldt);
      System.out.println("The month is: " + ldt.getMonthValue());
   }
}

输出

The LocalDateTime is: 2019-02-18T23:15:30
The month is: 2

现在,让我们了解一下上面的程序。

首先显示 LocalDateTime。然后,使用 getMonthValue() 方法获取一年中的月份,并将其显示出来。展示此方法的代码段如下 -

LocalDateTime ldt = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt);
System.out.println("The month is: " + ldt.getMonthValue());

更新于:2019-07-30

83 次浏览

开启你的 职业生涯

完成课程获取认证

开始吧
广告
© . All rights reserved.