Java Examples - Displaying months in short format

Advertisements


Problem Description:

How to display name of the months in short format?

Solution:

This example displays the names of the months in 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);
      }
   }
}

Result:

The above code sample will produce the following result.

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


Advertisements
Advertisements