Related Articles

Related Articles

LocalDateTime getMonth() method in Java with Examples
  • Last Updated : 29 Nov, 2018

The getMonth() method of an LocalDateTime class is used to return month-of-year field. This field is returned using the Month enum. The enum can provide the primitive int value.

Syntax:

public Month getMonth()

Parameter: This method does not accept any parameter.

Returns: This method returns the enum Month for the month of this LocalDateTime.

Below programs illustrate the LocalDateTime.getMonth() method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.getMonth() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2007-12-02T22:48:29");
  
        // get Month enum value field
        Month month = local.getMonth();
  
        // print result
        System.out.println("Month: "
                           + month);
    }
}

chevron_right


Output:

Month: DECEMBER

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.getMonth() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2017-07-22T09:32:42");
  
        // get Month enum value field
        Month month = local.getMonth();
  
        // print result
        System.out.println("Month: "
                           + month);
    }
}

chevron_right


Output:

Month: JULY

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :