Signup/Sign In
LAST UPDATED: NOVEMBER 24, 2020

Java LocalDate getMonthValue() Method

Java getMonthValue() method is used to get the month value of a date. It returns an integer value that represents the month. For example, we have a date 2012/02/15, then it will return 2 as a month value.

This method does not take any argument and returns an integer value. The syntax of the method is given below.

Syntax

public int getMonthValue()

Parameters

This method does not take any parameter.

Return

It returns an integer value.

Time For An Example

Let's take an example to get the value of a month as an integer value. In this example, we are using getMonthValue() that returns month as an integer value.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2015, 02, 15);
		System.out.println(localDate);
		int month = localDate.getMonthValue();
		System.out.println("Month of date : "+month);
	}
}


2015-02-15
Month of date : 2

Time For another Example

Let's take another example to understand the getMonthValue() method better. Here we are using the getMonthValue() method to get month value.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.ofYearDay(2020, 120);
		System.out.println(localDate);
		int month = localDate.getMonthValue();
		System.out.println("Month of date : "+month);
	}
}


2020-04-29
Month of date : 4

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.