本文共 2999 字,大约阅读时间需要 9 分钟。
lengthOfMonth() method is available in java.time package.
lengthOfMonth()方法在java.time包中可用。
lengthOfMonth() method is used to get the length of the month represented by this LocalDate. length of month = No. of days in a month [i.e. 1 to 31]
lengthOfMonth()方法用于获取此LocalDate表示的月份的长度。 月的长度=一个月中的天数[即1到31]
lengthOfMonth() 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.
lengthOfMonth()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
lengthOfMonth() method does not throw an exception at the time of returning length.
lengthOfMonth()方法在返回length时不会引发异常。
Syntax:
句法:
public int lengthOfMonth();
Parameter(s):
参数:
None
没有
Return value:
返回值:
The return type of this method is int, it returns the length of month denoted by this object.
此方法的返回类型为int ,它返回此对象表示的月份的长度。
Example:
例:
// Java program to demonstrate the example // of int lengthOfMonth() method of LocalDateimport java.time.*;public class LengthOfMonthOfLocalDate { public static void main(String args[]) { // Instantiates three LocalDate LocalDate l_da1 = LocalDate.parse("2007-04-04"); LocalDate l_da2 = LocalDate.now(); LocalDate l_da3 = LocalDate.of(2020, Month.FEBRUARY, 06); // Display l_da1,l_da2 and l_da3 System.out.println("LocalDate l_da1,l_da2 and l_da3: "); System.out.println("l_da1: " + l_da1); System.out.println("l_da2: " + l_da2); System.out.println("l_da3: " + l_da3); System.out.println(); // Here, this method returns the // length of month represented by // this date l_da1 i.e. here // l_da1 = "2007-04-04" then number // of days in the month of april is // 30 i.e. length = 30 int length = l_da1.lengthOfMonth(); // Display length System.out.println("l_da1.lengthOfMonth(): " + length); // Here, this method returns the // length of month represented by // this date l_da2 i.e. here // l_da2 = "2020-05-08" then number // of days in the month of may is // 31 i.e. length = 31 length = l_da2.lengthOfMonth(); // Display length System.out.println("l_da2.lengthOfMonth(): " + length); // Here, this method returns the // length of month represented by // this date l_da3 i.e. here // l_da3 = "2020-02-06" then number // of days in the month of feb is // either 28 or 29 based on year // i.e. 2020 is a leap year so the // number of days in feb is 29 i.e. // length = 29 length = l_da3.lengthOfMonth(); // Display length System.out.println("l_da3.lengthOfMonth(): " + length); }}
Output
输出量
LocalDate l_da1,l_da2 and l_da3: l_da1: 2007-04-04l_da2: 2020-05-31l_da3: 2020-02-06l_da1.lengthOfMonth(): 30l_da2.lengthOfMonth(): 31l_da3.lengthOfMonth(): 29
翻译自:
转载地址:http://vwvzd.baihongyu.com/