Find Month name using DateFormatSymbols class.


 

Find Month name using DateFormatSymbols class.

In this tutorial, you will learn how to find the month name using DateFormatSymbols class.

In this tutorial, you will learn how to find the month name using DateFormatSymbols class.

Find Month name using DateFormatSymbols class.

In this tutorial, you will learn how to find the month name using DateFormatSymbols class.

The class DateFormatSymbols encapsulates date-time formatting data, such as the names of the months, names of the days of the week, and the timezone data. Both the classes DateFormat and SimpleDateFormar use DateFormatSymbols class to encapsulate this information. Here we are going to find the month name based on the number entered by the user. The user is allowed to enter numbers between 1 and 12. The method getMonths() return all the months name in the form of array.

Example

import java.text.*;
import java.util.*;

class PrintMonth 
{
public static String getMonthForInt(int m) {
String month = "invalid";
DateFormatSymbols dfs = new DateFormatSymbols();
String[] months = dfs.getMonths();
if(m>=0&&m<=11) {
month = months[m];
}
return month;
}
public static void main(String[] args) 
{
Scanner input=new Scanner(System.in);
System.out.print("Enter number between 1 and 12: ");
int num=input.nextInt()-1;
System.out.println("Month is: "+getMonthForInt(num));
}
}

Output:

Enter number between 1 and 12: 7
Month is: July

Ads