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();