When Using Date Picker In Android It is Picking th

2020-02-07 13:17发布

When Using Date Picker In Android It is Picking the Wrong Date The date picker dialog is picking the date as 2018/55/10 It sometimes picks the right date but most of the time it is picking wrong date

Calendar myCalendar = Calendar.getInstance();
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();
        }

    };
    exp.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatePickerDialog d=new DatePickerDialog(createjobs.this, R.style.CalendarDatePickerDialog, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));
            d.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            d.show();
        }
    });

private void updateLabel() {
    String myFormat = "yyyy/mm/dd"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    exp.setText(sdf.format(myCalendar.getTime()));
}

1条回答
放荡不羁爱自由
2楼-- · 2020-02-07 13:41

You made a common mistake in your date format: mm is the minute, use MM to get the month.

查看更多
登录 后发表回答