我有使用datepicker的android代码。目前我得到的输出是11-2-14的形式。我希望格式为2-NOV-14.Below是我当前代码的一部分:
final Calendar c = Calendar.getInstance();
year=c.YEAR;
month=c.MONTH+1;
day=c.DAY_OF_MONTH;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID1:
// set date picker for current date
return new DatePickerDialog(this, pickerListener1, year, month,day);
case DATE_PICKER_ID2:
// set date picker for current date
return new DatePickerDialog(this, pickerListener2, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener1 = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
date1=""+day+"-"+month+"-"+year+" ";
Output1.setText(new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" "));
}
};
private DatePickerDialog.OnDateSetListener pickerListener2 = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
date2=""+day+"-"+month+"-"+year+" ";
Output2.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
以上代码的日期格式为11-2-14。请帮助。
答案 0 :(得分:0)
首先,您必须将当天与月份交换:
year = selectedYear;
month = selectedDay;
day = selectedMonth;
然后在类中添加一个私有静态final String数组作为字段,其中s [i]包含一年中第(i-1)个月的字符串represntation:s [10] =“NOV”和你替换
month = selectedDay;
与
month = s[selectedDay];
答案 1 :(得分:0)
尝试以下代码:
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
date1=""+day+"-"+month+"-"+year+" ";
String mon = formatMonth(month+1);
Output1.setText(day+"-"+mon+"-"+year);
}
};
public String formatMonth(int month) {
Locale locale;
DateFormat formatter = new SimpleDateFormat("MMM", locale);
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.MONTH, month-1);
return formatter.format(calendar.getTime());
}