Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
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.

[フレーム]



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.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /