问题
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.
回答1:
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].
回答2:
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
回答3:
Use a switch statement:
String monthName;
switch(mMonth){
case Calendar.JANUARY:
monthName = "January";
break;
case Calendar.FEBRUARY:
monthName = "February";
break;
etc.
回答4:
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.
回答5:
Example:
//if or swith
if (c.get(Calendar.MONTH)==(Calendar.FEBRUARY)) {
// Do something like
// String Month = "FEBRUARY";
}
回答6:
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.
来源:https://stackoverflow.com/questions/7940354/android-datepicker-display-month-name