Home Tutorial Java Core Find Month name using DateFormatSymbols class.

 
 

Share on Google+Share on Google+
Find Month name using DateFormatSymbols class.
Posted on: September 20, 2012 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for Find Month name using DateFormatSymbols class.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 20, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com