Java Program to Display Name of Months of Calendar Year in Short Format
As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names of the months to the shorter format, ie for eg, if the name of the month is October, then in the shorter format, it will be represented as October.
The main difference between the DateFormatSymbols class and SimpleDateFormat class is that DateFormatSymbols class enlists all the months of the calendar year whereas SimpleDateFormat class enlist the particular date of that month along with the current month and year.
Below are the ways discussed to convert the name of the month in the calendar year into the shorter format:
- Using DateFormatSymbols class
- Using SimpleDateFormat class
Example 1: Displaying months in shorter format Using DateFormatSymbols class
Java
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
public class GFG {
public static void main(String[] args)
{
DateFormatSymbols dateFormatSymbolsobject = new DateFormatSymbols();
String[] shortFormatMonthsNames = dateFormatSymbolsobject.getShortMonths();
for ( int i = 0 ;i < (shortFormatMonthsNames.length - 1 ); i++) {
String shortMonthName = shortFormatMonthsNames[i];
System.out.println( "Name of Month In Shorter Format "
+ shortMonthName);
}
}
}
|
Output
Name of Month In Shorter Format Jan
Name of Month In Shorter Format Feb
Name of Month In Shorter Format Mar
Name of Month In Shorter Format Apr
Name of Month In Shorter Format May
Name of Month In Shorter Format Jun
Name of Month In Shorter Format Jul
Name of Month In Shorter Format Aug
Name of Month In Shorter Format Sep
Name of Month In Shorter Format Oct
Name of Month In Shorter Format Nov
Name of Month In Shorter Format Dec
Example 2: Displaying months in shorter format Using SimpleDateFormat class
Java
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class GFG {
public static void main(String[] argv) throws Exception
{
String dateFormat = "dd-MMM-yyyy" ;
Date d = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
System.out.println(sdf.format(d));
}
}
|
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...