Java LocalDate类| 带示例的getMonthValue()方法

LocalDate类的getMonthValue()方法 (LocalDate Class getMonthValue() method)

  • getMonthValue() method is available in java.time package.

    getMonthValue()方法在java.time包中可用。

  • getMonthValue() method is used to get the month-of-year field value is an integer from this LocalDate.

    getMonthValue()方法用于从该LocalDate获取年份的月份值。

  • getMonthValue() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getMonthValue()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getMonthValue() method does not throw an exception at the time of getting the month of the year.

    getMonthValue()方法在获取一年中的月份时不会引发异常。

Syntax:

句法:

    public int getMonthValue();

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is int, it returns the month-of-year field value of this LocalDate and the number of months in a year starts from 1 to 12.

此方法的返回类型为int ,它返回此LocalDate的年月字段值,并且一年中的月数从1到12开始。

Example:

例:

// Java program to demonstrate the example 
// of getMonthValue() method of LocalDate

import java.time.*;
public class GetMonthValueOfLocalDate {
    public static void main(String args[]) {

        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-05");
        LocalDate l_da2 = LocalDate.now();

        // Display l_da1,l_da2
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method gets the value 
        // in int for the field month-of-year
        // in this date l_da1
        int get_val = l_da1.getMonthValue();

        // Display get_val
        System.out.println("l_da1.getMonthValue(): " + get_val);

        // Here, this method gets the value 
        // in int for the field month-of-year
        // in this date l_da2
        get_val = l_da2.getMonthValue();

        // Display get_val
        System.out.println("l_da2.getMonthValue(): " + get_val);
    }
}

Output

输出量

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-05
l_da2: 2020-05-30

l_da1.getMonthValue(): 4
l_da2.getMonthValue(): 5


翻译自: https://www.includehelp.com/java/localdate-getmonthvalue-method-with-example.aspx

Java 的 `LocalDate` 是一个表示日期的,它可以表示一个本地日期,没有时间和时区信息。它是 Java 8 中引入的一个日期/时间 API。 要使用 `LocalDate`,需要在代码中导入 `java.time.LocalDate`。 以下是一些常用的 `LocalDate` 方法: - `now()`:获取当前日期 - `of(int year, int month, int dayOfMonth)`:创建一个指定年份、月份和日期的 LocalDate 实例 - `getYear()`、`getMonthValue()` 和 `getDayOfMonth()`:获取年、月和日 - `plusDays(long daysToAdd)` 和 `minusDays(long daysToSubtract)`:增加或减少指定的天数,返回新的 LocalDate 实例 - `isEqual(LocalDate other)` 和 `isAfter(LocalDate other)`:比较两个 LocalDate 实例的大小关系 以下是一个示例代码: ```java import java.time.LocalDate; public class LocalDateExample { public static void main(String[] args) { // 获取当前日期 LocalDate today = LocalDate.now(); System.out.println("今天的日期是:" + today); // 创建一个指定日期的 LocalDate 实例 LocalDate date = LocalDate.of(2022, 11, 11); System.out.println("指定日期是:" + date); // 获取年、月、日 int year = date.getYear(); int month = date.getMonthValue(); int day = date.getDayOfMonth(); System.out.println("年:" + year + ",月:" + month + ",日:" + day); // 增加或减少指定的天数 LocalDate newDate = date.plusDays(100); System.out.println("增加 100 天后的日期是:" + newDate); // 比较两个 LocalDate 实例的大小关系 if (today.isEqual(date)) { System.out.println("今天和指定日期相同"); } else if (today.isAfter(date)) { System.out.println("今天在指定日期之后"); } else { System.out.println("今天在指定日期之前"); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值