Skip to content
Related Articles

Related Articles

Java Program to Display Name of Months of Calendar Year in Short Format
  • Last Updated : 05 Feb, 2021

As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names of the months to the shorter format, ie for eg, if the name of the month is October, then in the shorter format, it will be represented as October.

The main difference between the DateFormatSymbols class and SimpleDateFormat class is that DateFormatSymbols class enlists all the months of the calendar year whereas  SimpleDateFormat class enlist the particular date of that month along with the current month and year.

Below are the ways discussed to convert the name of the month in the calendar year into the shorter format:

  • Using DateFormatSymbols class
  • Using SimpleDateFormat class

Example 1: Displaying months in shorter format Using DateFormatSymbols class

Java




// Java program to convert the names of the months into the
// shorter format using DateFormatSymbols class
  
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
  
public class GFG {
    public static void main(String[] args)
    {
        // making the object of the DateFormatSymbols class
        DateFormatSymbols dateFormatSymbolsobject = new DateFormatSymbols();
        
        // calling the method of the DateFormatSymbols class
        String[] shortFormatMonthsNames = dateFormatSymbolsobject.getShortMonths();
  
        for (int i = 0;i < (shortFormatMonthsNames.length - 1); i++) {
            
            // getting the month name  from particular index
            String shortMonthName = shortFormatMonthsNames[i];
            
            System.out.println("Name of Month In Shorter Format "
                + shortMonthName);
        }
    }
}
Output



Name of Month In Shorter Format Jan
Name of Month In Shorter Format Feb
Name of Month In Shorter Format Mar
Name of Month In Shorter Format Apr
Name of Month In Shorter Format May
Name of Month In Shorter Format Jun
Name of Month In Shorter Format Jul
Name of Month In Shorter Format Aug
Name of Month In Shorter Format Sep
Name of Month In Shorter Format Oct
Name of Month In Shorter Format Nov
Name of Month In Shorter Format Dec

Example 2: Displaying months in shorter format Using SimpleDateFormat class

Java




// Java program to dispaly the name of the month in shorter
// format using SimpleDateFormat class
  
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
  
public class GFG {
    public static void main(String[] argv) throws Exception
    {
        // The format of the date which we want to display
        String dateFormat = "dd-MMM-yyyy";
        
        // getting the date using getTime() method of the
        // Calendar class
        Date d = Calendar.getInstance().getTime();
        
        // getting the date in the form of dateFormat String
        // that we have mentioned above we have mentioned
        // Locale.English explicitly since,if we write
        // Locale.FRENCH,then we would have get the date in
        // some other notation
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        
        // printing the date with the month name in the
        // shorter format
        System.out.println(sdf.format(d));
    }
}
Output
01-Feb-2021

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :