LocalDateTime getMonthValue() method in Java with Examples

The getMonthValue() method of LocalDateTime class is used to return the month-of-year field. This method returns the month from LocalDateTime as an int from 1 to 12.

Syntax:

public int getMonthValue()

Parameter: This method does not accept any parameter.

Returns: This method returns integer value which is the month-of-year field and it ranges from 1 to 12.

Below programs illustrate the LocalDateTime.getMonthValue() method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.getMonthValue() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2018-10-23T22:29:10");
  
        // get Month value field
        int monthvalue = local.getMonthValue();
  
        // print result
        System.out.println("Month Value: "
                           + monthvalue);
    }
}

chevron_right


Output:

Month Value: 10

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.getMonthValue() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2018-12-13T12:28:13");
  
        // get Month value field
        int monthvalue = local.getMonthValue();
  
        // print result
        System.out.println("Month Value: "
                           + monthvalue);
    }
}

chevron_right


Output:

Month Value: 12

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#getMonthValue()

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.