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


A year has 12 months which are named January, February, March, April, May, June, July, August, September, October, November, December. These are the complete names of the months and there are multiple ways to represent them like short format, complete format, MM or 2 integers format. In this article, we are going to see how to display names of months of calendar year in short format with the help of Java. There are numerous ways to do so and are as follows −

  • Using a String Array

  • Using the java.time.Month Enum

  • Using the java.text.DateFormatSymbols Class

  • Using a Map

  • Using the SimpleDateFormat Class

  • Using the java.util.Calendar Class

Let us now look at their implementations.

Approach 1: Using a String Array

This is also called the Naïve approach in which we make use of String array which consists of the short format names of months hardcoded as it's elements. A for loop is then used to iterate over the array and display all months.

Example

public class MonthNames {
   public static void main(String[] args) {
      String[] short_format_months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
      System.out.println("Short format of calendar months:");
      for (String month : short_format_months) {
         System.out.println(month);
      }
   }
}

Output

Short format of calendar months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Approach 2: Using the java.time.Month Enum

The java.time.Month enum which is an enumeration in the java.time package, was introduced in Java 8. It represents the 12 months of the year and provides some useful methods to work with months in Java. With the help of the enum we fetch the names of the months is short format and use a for loop to display each month name.

Example

import java.time.Month;
public class MonthNames2 {
   public static void main(String[] args) {
      System.out.println("Short format of calendar months:");
      for (Month month : Month.values()) {
         System.out.println(month.toString().substring(0,1)+""+(month.toString().substring(1,3).toLowerCase()));
      }
   }
}

Output

Short format of calendar months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Approach 3: Using the java.text.DateFormatSymbols Class

The Java.text.DateFormatSymbols class is an in-built Java library that provides access to symbols used in date and time formatting, such as month names, day of week names, and am/pm strings. It allows formatting dates and times in various locales according to specified locales.

The DateFormatSymbols class provides methods to retrieve the names of months in various forms, such as full names, abbreviated names and standalone ones. It also offers methods for retrieving day of week names and other symbols used in date and time formatting.

This class is especially helpful when working with localized date and time data, where the format and symbols used may differ based on language and region.

Here, we will utilize the Java.text.DateFormatSymbols class to obtain month names in short formats and iterate over this array using a for loop.

Example

import java.text.DateFormatSymbols;
public class MonthNames3 {
   public static void main(String[] args) {
      DateFormatSymbols symbols = new DateFormatSymbols();
      String[] months = symbols.getShortMonths();
      System.out.println("Short format of calendar months:");
      for (String month : months) {
         System.out.println(month);
      }
   }
}

Output

Short format of calendar months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Approach 4: Using the Map

Map is an interface in the java.util package that represents a collection of key-value pairs. It allows quick and efficient lookup of values based on their associated keys.

Maps work like Sets, only instead of storing individual elements they store pairs - one serving as the key and another providing the value. By using a key that can be quickly retrieved with its corresponding value in constant time, Maps become an efficient data structure for many purposes.

This method requires the names of months to be hardcoded and then retrieved using keys.

Example

import java.util.HashMap;
import java.util.Map;

public class MonthNames4 {
   public static void main(String[] args) {
      Map<Integer, String> months = new HashMap<>();
      months.put(1, "Jan");
      months.put(2, "Feb");
      months.put(3, "Mar");
      months.put(4, "Apr");
      months.put(5, "May");
      months.put(6, "Jun");
      months.put(7, "Jul");
      months.put(8, "Aug");
      months.put(9, "Sep");
      months.put(10, "Oct");
      months.put(11, "Nov");
      months.put(12, "Dec");

      System.out.println("Short format of calendar months:");
      for (int i = 1; i <= 12; i++) {
         System.out.println(months.get(i));
      }
   }
}

Output

Short format of calendar months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Approach 5: Using the SimpleDateFormat Class

Java's SimpleDateFormat class, found in the java.text package, allows developers to format and parse dates and times according to a specified pattern. Before using SimpleDateFormat, a pattern string must first be defined that specifies desired date/time format; this pattern can include letters or symbols representing various components such as year, month, day, hour, minute, and second.

Once a pattern string is defined, SimpleDateFormat can be utilized to format or parse an object into a Date object according to that specified pattern. It provides methods such as format(Date date) and parse(String source) for this purpose.

Example

import java.text.SimpleDateFormat;
import java.util.Date;
public class MonthNames5 {
   public static void main(String[] args) {
      SimpleDateFormat formatter = new SimpleDateFormat("MMM");

      System.out.println("Short format of calendar months:");
      for (int i = 0; i < 12; i++) {
         Date date = new Date(0, i, 1);
         System.out.println(formatter.format(date));
      }
   }
}

Output

Short format of calendar months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Appraoch 6: Using the java.util.Calendar Class

The Java.util.Calendar class is an essential Java library providing date and time processing functionality. It can be used for calculations and manipulations on dates and times, such as adding or subtracting days, weeks, months or years; plus extracting specific fields like the year, month or day.

Calendar allows you to quickly create a new instance of the current date and time, or set it to any specific time. With its many methods, you can perform various operations on the calendar like setting fields or getting data, calculating differences between dates, or formatting dates for display.

This class can handle various calendar systems such as Gregorian and Julian, along with supporting different time zones. Unfortunately, its usage and comprehension may prove complex for some developers; furthermore, it isn't thread-safe.

Example

import java.util.Calendar;
import java.text.SimpleDateFormat;  
public class MonthNames6 {
   public static void main(String[] args) {
      String[] months = new String[12];
      Calendar cal = Calendar.getInstance();
      for (int i = 0; i < 12; i++) {
         cal.set(Calendar.MONTH, i);
         months[i] = new SimpleDateFormat("MMM").format(cal.getTime());
      }
      System.out.println("Short format of calendar months:");
      for (String month : months) {
         System.out.println(month);
      }
   }
}

Output

Short format of calendar months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Conclusion

The 12 months can be represented in various formats out of which short format is one of the methods. There are 6 methods to display names of months of calendar year in short format. Each method has its own set of pros and cons and the choice of method is dependent on multiple factors like code readability, performance, and requirements of the specific use case. Irrespective of which method is implemented, the short format of each month will remain the exact same. This program is a useful skill for any Java developer working with date and time data.

Updated on: 06-Apr-2023

420 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements