Reputation: 161
Hi I would like to display the month name when I click in my DatePickerDialog but it's display the month number
Here is my code
tvDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int day = calendar.get(Calendar.DAY_OF_WEEK);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
dpd = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int nYear, int nMonth, int nDay) {
calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
tvDate.setText(nDay + "/" + (nMonth+1) + "/" + nYear);
}
}, year, month, day);
dpd.show();
}
});
Upvotes: 0
Views: 3581
Reputation: 164214
You can format the date you get from DatePickerDialog
to display the name of the month:
public void onDateSet(DatePicker datePicker, int nYear, int nMonth, int nDay) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMMM/yyyy");
calendar.set(nYear, nMonth, nDay);
String dateString = sdf.format(calendar.getTime());
tvDate.setText(dateString);
}
Upvotes: 3
Reputation: 5370
There are two ways of getting month name
Method 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]. Ref
Method 2 Setting the current date in the Calendar and getting back the month like this:
@Override
public void onDateSet(DatePicker datePicker, int nYear, int nMonth, int nDay) {
calendar.set(Calendar.DAY_OF_MONTH,nDay);
calendar.set(Calendar.MONTH,nMonth);
calendar.set(Calendar.YEAR,mYear);
calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
tvDate.setText(nDay + "/" + (nMonth+1) + "/" + nYear);
}
In my personal opinion, I would recommend the Method 1
Upvotes: 2