Wednesday, July 3, 2024
HomeLanguagesJavaLocalDateTime getMonthValue() method in Java with Examples

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:




// 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);
    }
}


Output:

Month Value: 10

Program 2:




// 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);
    }
}


Output:

Month Value: 12

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments