How to display name of the months in short format using Java



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

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

import java.text.Format;
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";
      Date d = Calendar.getInstance().getTime();
      SimpleDateFormat sdf = new SimpleDateFormat(str1, Locale.FRENCH);
      System.out.println(sdf.format(d));
      sdf = new SimpleDateFormat(str1, Locale.ENGLISH);
      System.out.println(sdf.format(d));
   }
}

Result

The above code sample will produce the following result(Result is depends on Current date.

11-nov.-16
11-Nov-16
java_date_time.htm
Advertisements