如何在android datepicker中实现01 / Jan / 2000格式

时间:2016-07-04 11:25:44

标签: android datepicker date-format

我试图在android / datexicker中的01 / Jan / 2000中实现日期格式,为此我编写了以下代码:

    alist.add(0, "Jan");
    alist.add(1, "Feb");
    alist.add(2, "Mar");
    alist.add(3, "Apr");
    alist.add(4, "May");
    alist.add(5, "Jun");
    alist.add(6, "Jul");
    alist.add(7, "Aug");
    alist.add(8, "Sep");
    alist.add(9, "Oct");
    alist.add(10, "Nov");
    alist.add(11, "Dec");

 Calendar mcurrentDate = Calendar.getInstance();
int mYear, mMonth = 0, mDay;
  String arr[] = dob.split("-");
        mYear = Integer.parseInt(arr[2].trim());
        try {
            mMonth = Integer.parseInt(arr[1].trim()) - 1;
        } catch (Exception e)
        {

        }
        mDay = Integer.parseInt(arr[0].trim());
    } else {
        mYear = mcurrentDate.get(Calendar.YEAR);
        mMonth = mcurrentDate.get(Calendar.MONTH);
        mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
    }
    final DatePickerDialog mDatePicker = new DatePickerDialog(ProfileActivity.this, R.style.datepicker, new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
            int month = selectedmonth + 1;
            String formattedDayOfMonth = "";
            String newmonth = "";
            if (DateHelper.checkPastDate(selectedday + "-" + selectedmonth + "-" + selectedyear)) {
                if (selectedday < 10) {
                    formattedDayOfMonth = "0" + String.valueOf(selectedday);
                } else
                    formattedDayOfMonth =  String.valueOf(selectedday);

                for (int i = 0; i < alist.size(); i++) {
                    if (selectedmonth == i) {
                        newmonth = (String) alist.get(i);
                        break;
                    }
                }
                changeDOB.setText(formattedDayOfMonth + "-" + newmonth + "-" + selectedyear);
                user.setDob((selectedday + "-" + selectedmonth + "-" + selectedyear));
            } else {
                getApp().showToast("please select valid date");
            }
        }
    }, mYear, mMonth, mDay);
    mDatePicker.setTitle("Select date");
    mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
    mDatePicker.show();
}

但是在这里,我正在遵循以下例外:

java.lang.NumberFormatException: Invalid int: "Jan"

在线:

 mMonth = Integer.parseInt(arr[1].trim()) - 1;

请帮帮我解决这个问题。谢谢。

5 个答案:

答案 0 :(得分:0)

试试这个

 Calendar now = Calendar.getInstance();

                DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
                                                                        @Override
                                                                        public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                                                                            String month = "", day = "";
                                                                            if (monthOfYear < 10) {
                                                                                month = "0" + (monthOfYear + 1);
                                                                            } else {
                                                                                month = String.valueOf(monthOfYear + 1);
                                                                            }

                                                                            if (dayOfMonth < 10) {
                                                                                day = "0" + dayOfMonth;
                                                                            } else {
                                                                                day = String.valueOf(dayOfMonth);
                                                                            }

                                                                            String date = year + "/" + (month) + "/" + (day);
                                                                            edAddnewsMissingdate.setText(date);
                                                                        }
                                                                    }, now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH));

                dpd.show(getFragmentManager(), "Datepickerdialog");
                dpd.setMaxDate(Calendar.getInstance());
                break;

答案 1 :(得分:0)

我建议您浏览this文档。之后,您要搜索的日期格式为 dd / MMM / yyyy

答案 2 :(得分:0)

使用 SimpleDateFormat 格式化和解析日期。这样,您可以使用所需的格式轻松地从日历中保存日期,稍后您可以将其转换回日历对象。

答案 3 :(得分:0)

尝试这种方式获得相同的格式

et1.setOnClickListener(new View.OnClickListener() {
            // @Override
            public void onClick(View v) {
                final Calendar c = Calendar.getInstance();

                int y = c.get(Calendar.YEAR)+4;
                int m = c.get(Calendar.MONTH)-2;
                int d = c.get(Calendar.DAY_OF_MONTH);
                final String[] MONTH = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

                DatePickerDialog dp = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                                String erg = "";
                                erg = String.valueOf(dayOfMonth);
                                erg += "." + String.valueOf(monthOfYear + 1);
                                erg += "." + year;

                                ((TextView) et1).setText(erg);

                            }

答案 4 :(得分:0)

您应该使用CalendarSimpleDateFormat来设置日期格式。

我已经测试了以下代码,它可以提供所需的格式。

Calendar cal = new GregorianCalendar();
cal.set(selectedyear, selectedmonth, selectedday);

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
dateFormat.setCalendar(cal);
String date = dateFormat.format(cal.getTime());

您可以按如下方式更新代码以设置DOB:

final DatePickerDialog mDatePicker = new DatePickerDialog(ProfileActivity.this, R.style.datepicker, new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
            int month = selectedmonth + 1;
            String formattedDayOfMonth = "";
            String newmonth = "";
            if (DateHelper.checkPastDate(selectedday + "-" + selectedmonth + "-" + selectedyear)) {
                Calendar cal = new GregorianCalendar();
                cal.set(selectedyear, selectedmonth, selectedday);

                SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
                dateFormat.setCalendar(cal);

                user.setDob(dateFormat.format(cal.getTime()));
            } else {
                getApp().showToast("please select valid date");
            }
        }
    }, mYear, mMonth, mDay);