Java LocalDate Class | lengthOfMonth() Method with Example

LocalDate Class lengthOfMonth() method: Here, we are going to learn about the lengthOfMonth() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on May 31, 2020

LocalDate Class lengthOfMonth() method

  • lengthOfMonth() method is available in java.time package.
  • 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() 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() method does not throw an exception at the time of returning 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.

Example:

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

import 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-04
l_da2: 2020-05-31
l_da3: 2020-02-06

l_da1.lengthOfMonth(): 30
l_da2.lengthOfMonth(): 31
l_da3.lengthOfMonth(): 29


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.