Java Program to Display Name of Months of Calendar Year in Short Format - GeeksforGeeks

20 Apr, 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);        }    }}
  
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 display 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));    }}
  
01-Feb-2021
 
AshokJaiswal
Java-Date-Time
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java