When I implement a DatePickerDialog it display the correct date but when I click OK to set the date in an EditText I get a date String with incorrect month, which seems to be random each time I try
final Calendar calendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy", Locale.getDefault());
String newDate = dateFormat.format(new Date(calendar.getTimeInMillis()));
Log.e(TAG,"date: "+newDate);
mTextSetDate.setText(newDate);
}
};
And to display the DatePickerDialog, I have this function
private void showDatePicker(DatePickerDialog.OnDateSetListener listener, Calendar cal){
new DatePickerDialog(AddNewRecordActivity.this,listener,
cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)).show();
}
and here's the log output
09-26 08:20:13.511 4240-4240/tz.co.neelansoft.personalaccountant E/AddNewRecordActivity: date: 26-18-2018
I included some screenshot of datepickerdialog and date string