Java Program to list Short Month Names

Java 8Object Oriented ProgrammingProgramming

To list short months, use the getShortMonths() from the DateFormatSymbols class in Java.

DateFormatSymbols is a class for encapsulating localizable date-time formatting data.

Get short month names in an array

String[] months = new DateFormatSymbols().getShortMonths();

Display the months

for (int i = 0; i < months.length - 1; i++) {
String month = months[i];
System.out.println("Month ["+i+"] = " + month);
}

The following is an example −

Example

 Live Demo

import java.text.DateFormatSymbols;
   public class Demo {
      public static void main(String[] args) {
      // short months
      String[] months = new DateFormatSymbols().getShortMonths();
      for (int i = 0; i < months.length - 1; i++) {
         String month = months[i];
         System.out.println("Month ["+i+"] = " + month);
      }
   }
}

Output

Month [0] = Jan
Month [1] = Feb
Month [2] = Mar
Month [3] = Apr
Month [4] = May
Month [5] = Jun
Month [6] = Jul
Month [7] = Aug
Month [8] = Sep
Month [9] = Oct
Month [10] = Nov
Month [11] = Dec
raja
Updated on 27-Jun-2020 13:15:02

Advertisements