如何在android Datepicker上以字符串格式显示日期

时间:2015-05-11 11:11:29

标签: android widget


我的y - 1日期显示为05-12-2015MM-dd-yyyy格式为TextView Textview旁边有一个按钮,点击后会显示当前日期为6th may的datepicker 但我希望datepicker显示5th may而不是当前日期 任何人都可以帮我解决这个问题。

3 个答案:

答案 0 :(得分:1)

datePicker.init(2015, 12, 5, null);

答案 1 :(得分:1)

你可以像

一样使用它
Calendar myCalendar = Calendar.getInstance();

然后从onCreate()

调用它
   new DatePickerDialog(Electricity_Activity.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();

然后编写此代码只需替换

  myCalendar.set(Calendar.YEAR, year); 

与您的年,月,日

 DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel();
            }
            private void updateLabel() {
                   String myFormat = "dd/MM/yyyy"; //In which you need put here
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                    ed_date.setText(sdf.format(myCalendar.getTime()));
              }

    };

答案 2 :(得分:1)

SimpleDateFormat formatter = new SimpleDateFormat(“MM-dd-yyyy”,Locale.getDefault());
                                        // formatter.setTimeZone(TimeZone.getTimeZone(“UTC”));
formatter.setLenient(假);