1

I am trying to get the selected date from the datepicker but I could not get the correct value. My code is given below.

View mView = getLayoutInflater().inflate(R.layout.set_date_layout, null);

    dialog = new MaterialDialog.Builder(RemainderActivity.this);
    dialog.title(R.string.set_date);

    datePicker = (DatePicker) mView.findViewById(R.id.datePickerRemainder);


    dialog.customView(mView, true);
    dialog.positiveText(R.string.confirm_date);
    dialog.negativeText(android.R.string.cancel);
    dialog.cancelable(false);
    dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

            DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            Log.d("YEAR", datePicker.getYear()+""); //Shows correct year as 2017
            Date date = new Date(datePicker.getDayOfMonth(), (datePicker.getMonth() + 1),datePicker.getYear()); // Wrong date as 09-04-1907
            String dateString = df.format(date);
            txt_remainder_date_add.setText(dateString);
        }
    });
    dialog.build();
    dialog.show();

2 Answers2

0
  private void datepicker() {
    // Get Current Date
    final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog datePickerDialog = new DatePickerDialog(this, R.style.DialogTheme,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
txt_remainder_date_add.setText(date_time);
                }
            }, mYear, mMonth, mDay);
    datePickerDialog.show();
}

where my DialogTheme defined in style :

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/colorPrimary</item>
</style>

call this method where ever you want

Shashwat Gupta
  • 876
  • 9
  • 22
  • yes @SharaafNazeer it will work completely fine upto api15 to 25 – Shashwat Gupta Sep 01 '17 at 11:45
  • The out put I am getting here is 2-9-2017.. How can I make it as 02-09-2017?. – Sharaaf Nazeer Sep 02 '17 at 16:04
  • I could not format it.. I'm getting following error java.lang.IllegalArgumentException: Bad class: class java.lang.String This is my code segment global_date = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year; DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); String dateString = df.format(global_date); txt_remainder_date_add.setText(dateString); – Sharaaf Nazeer Sep 02 '17 at 16:28
  • Please remove - from string then try – Shashwat Gupta Sep 02 '17 at 16:32
0

According to this Link

you have used deprecated code in below line :

Date date = new Date(datePicker.getDayOfMonth(), (datePicker.getMonth() + 1),datePicker.getYear());

Instead of using Date class try Calendar class object like this...

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(cal.getTime()));

you can refer this Link for example...

Intimate
  • 446
  • 2
  • 5
  • 18