Reputation: 54949
I am trying use Date Picker and on Select i want to display the date in the following format
[Month name] [date], [year]
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR)-13;
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
that gives Month as a number. How to get the name of the month instead of number.
Upvotes: 6
Views: 16339
Reputation: 29121
public static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
use an array and get the String by MONTHS[monthNumber]
.
Upvotes: 23
Reputation: 31
After getting the year, month and day, you can format your date this way:
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat.MEDIUM makes the date displayed as Jan 12, 1952, in example. If you want to display the month's full name, you can use DateFormat.LONG.
Upvotes: 2
Reputation: 31
After getting the year, month and day, you can format your date this way:
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat.MEDIUM makes the date displayed as Jan 12, 1952, in example. If you want to display the month's full name, you can use DateFormat.LONG.
This is the easiest way, in my opinion.
Upvotes: 1
Reputation: 1321
Try this:
final Calendar c = Calendar.getInstance();
c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
output will be January
if Month == 1;
Or you can use this
c.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
Then you get Jan
Upvotes: 7
Reputation: 356
Example:
//if or swith
if (c.get(Calendar.MONTH)==(Calendar.FEBRUARY)) {
// Do something like
// String Month = "FEBRUARY";
}
Upvotes: 1
Reputation: 2645
Use a switch statement:
String monthName;
switch(mMonth){
case Calendar.JANUARY:
monthName = "January";
break;
case Calendar.FEBRUARY:
monthName = "February";
break;
etc.
Upvotes: 3