Java LocalDate getDayOfMonth() Method with Examples

In this article we are going to see the use of Java LocalDate class getDayOfMonth() method with suitable examples.

Java LocalDate getDayOfMonth() Method with Examples

Explanation:

This java.time.LocalDate.getDayOfMonth() method is used to get the day of the month which date is given by the user. It returns the day of the month which is from 1 to 31.

Syntax:

public int getDayOfMonth()

Let’s see some example programs to understand it more clearly.

Approach:

  • Create an object of localDate class.
  • Then use the getDayOfMonth() method for this particular date to extract the day of that month.
  • Print the final result.

Example-1:

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-11");
        //use the getDayOfMonth() method and print the result
      	System.out.println("Day-of-the-month: "+date.getDayOfMonth()); 
    }
}
Output:

Day-of-the-month: 11

Example-2:

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-29");
        //use the getDayOfMonth() method and print the result
          System.out.println("Day-of-the-month: "+date.getDayOfMonth()); 
    }
}
Output:

Day-of-the-month: 29

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.