Write a Java program to get the information of current/given month


This program demonstrates how to get various information about the current month using the Month class of the java.time package.

The program first creates a LocalDate object representing the date "June 8, 2014". It then calls the getMonth() method on this object to obtain a Month object representing the month of June.

The program then obtains several pieces of information about this month using the following methods of the Month class:

  • getValue(): returns the numeric value of the month. In this case, the value is 6, since June is the 6th month of the year.
  • minLength(): returns the minimum number of days in the month. In this case, the value is 30, since June has 30 days.
  • maxLength(): returns the maximum number of days in the month. In this case, the value is also 30, since June has 30 days.
  • firstMonthOfQuarter(): returns the first month of the quarter that this month belongs to. In this case, June is the second month of the quarter, so the method returns Month.APRIL, the first month of the quarter.

Finally, the program prints out these pieces of information about the current month using System.out.println().

Source Code

import java.time.*;
class Get_Information_CurrentMonth
{
	public static void main(String[] args)
	{	
		LocalDate ldt = LocalDate.of(2014, Month.JUNE, 8);
		Month m = ldt.getMonth();
		int cm = m.getValue();
		int dm = m.minLength();
		int mdm = m.maxLength();
		Month firstMonthOfQuarter = m.firstMonthOfQuarter();
		System.out.println("Current Month of Number : " + cm);
		System.out.println("Number of Days in Month : " + dm);
		System.out.println("Maximum Number of Days in Month : " + mdm); 
		System.out.println("First Month of the Quarter : " + firstMonthOfQuarter); 
	}
}

Output

Current Month of Number : 6
Number of Days in Month : 30
Maximum Number of Days in Month : 30
First Month of the Quarter : APRIL

Example Programs