嗨,我想在我的DatePickerDialog中单击时显示月份名称,但它会显示月份编号
这是我的代码
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();
}
});
答案 0 :(得分:2)
您可以格式化从DatePickerDialog
获得的日期以显示月份名称:
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);
}
答案 1 :(得分:1)
有两种获取月份名称的方法
方法1
public static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
使用数组,并按MONTHS [monthNumber]获取字符串。 Ref
方法2 在日历中设置当前日期并像这样返回月份:
@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);
}
我个人认为,我推荐方法1