LAST UPDATED: NOVEMBER 24, 2020
Java LocalDate getMonth() Method
Java getMonth()
method is used to get the month of a date. It returns the full name of the month like January, February, etc. For example, we have a date 2012/10/12, then the result would be October.
This method belongs to the LocalDate
class which is located in java.time
package. The syntax of the example is given below.
Syntax
public Month getMonth()
Parameter
It does not take any parameter.
Return
It returns the month-of-year.
Time for an Example:
Lets take an example to get month name by using the getMonth()
method. Here we are getting month of a date, the method returns a month of Month enum.
import java.time.LocalDate;
import java.time.Month;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2015, 10, 21);
System.out.println(localDate);
Month month = localDate.getMonth();
System.out.println("Month of date : "+month);
}
}
2015-10-21
Month of date : OCTOBER
Time for another Example:
Lets take another example in which we are getting month name from a date that does not contain month field, but the month is capable to get month. See the below example.
import java.time.LocalDate;
import java.time.Month;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.ofYearDay(2020, 120);
System.out.println(localDate);
Month month = localDate.getMonth();
System.out.println("Month of date : "+month);
}
}
2020-04-29
Month of date : APRIL
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.