我无法从android中的日期选择器中选择年份?

时间:2015-10-21 13:35:44

标签: android android-datepicker

我在代码中使用日期选择器来选择出生日期。我每天都会被选中,但在获取年份时会面临问题。我希望日期为 MM / dd / yyyy 格式。

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final DatePicker picker = new DatePicker(getActivity());
picker.setCalendarViewShown(false);
final Calendar c =Calendar.getInstance();

picker.setMaxDate(c.getTime().getTime());
builder.setView(picker)
        .setPositiveButton("UPDATE",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                picker.setMaxDate(c.getTime().getTime());
                final SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
                final Date d=new Date();
                d.setYear(picker.getYear());d.setMonth(picker.getMonth());d.setDate(picker.getDayOfMonth());
                String s=sdf.format(d);
                dob.setText(s);
                Log.e("DOB",s);
            }
        })
        .setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        })
        .create()
        .show();

1 个答案:

答案 0 :(得分:1)

直接使用DatePickerDialog

DatePickerDialog dpd = new DatePickerDialog(getActivity(), onDateListener, year, month, day);