How to Display Name of the Months in Short Format Using Java



In this tutorial, we will format the current month using the full month name (MMMM format) using the SimpleDateFormat class in Java. By specifying the pattern "MMMM", you can retrieve and display the month's name in full, like "January", "February", etc. The following are the different ways to display the names of months in a short format

  • Using DateFormatSymbols to Display Short Month Names

  • Formatting Dates in Different Locales

Using DateFormatSymbols to Display Short Month Names

DateFormatSymbols class provides language-sensitive information about the formatting and parsing of dates and times, including month names.

getShortMonths(): The getShortMonths method of DateFormatSymbols returns an array of the short month names (e.g., "Jan", "Feb", etc.).

Example

The following example displays the names of the months in a short form with the help of DateFormatSymbols().getShortMonths() method of DateFormatSymbols class

import java.text.SimpleDateFormat;
import java.text.DateFormatSymbols;

public class Main {
   public static void main(String[] args) {
      String[] shortMonths = new DateFormatSymbols().getShortMonths();
      
      for (int i = 0; i < (shortMonths.length-1); i++) {
         String shortMonth = shortMonths[i];
         System.out.println("shortMonth = " + shortMonth);
      }
   }
}

Output

shortMonth = Jan
shortMonth = Feb
shortMonth = Mar
shortMonth = Apr
shortMonth = May
shortMonth = Jun
shortMonth = Jul
shortMonth = Aug
shortMonth = Sep
shortMonth = Oct
shortMonth = Nov
shortMonth = Dec

Formatting Dates in Different Locales

Locale: A class that represents a specific geographical, political, or cultural region. It affects how certain information (like month names or date formats) is displayed. The Locale.FRENCH and Locale.ENGLISH represents French and English-speaking regions, respectively.

Calendar getInstance(): The Java Calendar getInstance() method gets a calendar using the current time zone and locale.

syntax

The following is the syntax for Calendar getInstance()

public static Calendar getInstance()

Example

The following is another example of Date, Time, and short month

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;

public class Main {
   public static void main(String[] argv) throws Exception {
      String str1 = "dd-MMM-yy";  // Format pattern
      Date d = Calendar.getInstance().getTime();  // Get the current date and time
      
      // Formatting for French Locale
      SimpleDateFormat sdf = new SimpleDateFormat(str1, Locale.FRENCH);
      System.out.println("French Locale: " + sdf.format(d));  // Output in French Locale
      
      // Formatting for English Locale
      sdf = new SimpleDateFormat(str1, Locale.ENGLISH);
      System.out.println("English Locale: " + sdf.format(d));  // Output in English Locale
   }
}

Output

French Locale: 09-d?c.-24
English Locale: 09-Dec-24
Advertisements