本文介绍了在Android中使用日期选择器时,选择错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中使用日期选择器时,它选择了错误的日期日期选择器对话框将日期选择为 2018/55/10 它有时会选择正确的日期,但大多数情况下会选择错误的日期

When Using Date Picker In Android It is Picking the Wrong DateThe date picker dialog is picking the date as 2018/55/10It 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()));
}

推荐答案

您在日期格式中犯了一个常见错误:mm是分钟,请使用MM来获取月份.

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

这篇关于在Android中使用日期选择器时,选择错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:03