Java Program to Display Current Month in the (MMMM) Format

In the previous article, we have discussed about Java Program to Display Current Date and Time

In this article we are going to see how to display current month in the (MMMM) format in Java along with suitable examples.

Java Program to Display Current Month in the (MMMM) Format

Let’s see the program to understand it clearly.

Method-1: Java Program to Display Current Month in the (MMMM) Format By Using SimpleDateFormat Class

The java.text.SimpleDateFormat class provides inbuild methods to format the date and time in java.

There are 2 patterns which we can use in SimpleDateFormat to display the month.

  1. MMM – used for displaying month in 3 letters.
  2. MMMM – used for displaying month in complete abbreviation.

Approach:

  • Create an object of SimpleDateFormat as ‘s’ with the argument as ‘MMMM’.
  • Declare a string variable as ‘month’ and initialize it to the current date and time using an inbuild method of SimpleDateFormat as s.format(new Date())
  • Print the result.

Program:

import java.text.SimpleDateFormat;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        //create an object of SimpleDateFormat as ‘s’ with the argument as ‘MMMM’.
        SimpleDateFormat s = new SimpleDateFormat("MMMM");
        // Declare a string variable as ‘month’ and initialize it to the current date and time using an inbuild method of SimpleDateFormat as s.format(new Date())
        String month = s.format(new Date());
        //Print the result in MMMM format
        System.out.println("Month in MMMM format = "+month);
    }
}
Output:

Month in MMMM format = June

Method-2: Java Program to Display Current Month in the (MMMM) Format By Using Calendar and Formatter Class

The java.util.Formatter class provides inbuild “.format()” methods to format the month in java.

The java.util.Calendar class provides an inbuild method “.getInstance()” method to get the instant date-time-month from the system calendar.

There are 3 patterns which we can use in Formatter to display the month.

  1. %tb – used for displaying month in 3 letters.
  2. %tB – used for displaying month in complete abbreviation.
  3. %tm – used for displaying month number.

Approach:

  • Declare a variable ‘cal’ of type Calendar and initialize it to get the system date-time using Calender.getInstance() method.
  • Create object of Formatter as ‘f1’ and initialize it to ‘f1.format("%tB",cal)’ to store the month in ‘MMMM‘ format.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        //declare a variable ‘cal’ of type Calender and initialize it to get the system date time month using Calender.getInstance() method.
      	Calendar cal = Calendar.getInstance();
        //create an object of Formatter as ‘f1
      	Formatter f1 = new Formatter();
        //store the month format in f1 variable
      	f1.format("%tB",cal);
        //Print the result
      	System.out.println("Month in MMMM format ="+f1);
    }
}
Output:

Month in MMMM format =June

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: