I have a button that displays a calendar for the user to pick his/her date of birth but my app crashes if the user pick a date like "9-9-2022" that's if the day or month picked is less than two digits because the backend is expecting something of this kind "09-09-2020". I tried to add zero to the month and day if the selected day or month is less than 10 but it's still not working. Please how should I solve the issue? Here is the code.
private void setDate(){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
android.app.DatePickerDialog dialog = new DatePickerDialog(
PatientRegistrationActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d("Main", "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);
//this is where I want to add 0 to the selected day and time if it's less than 10
if (month<10){
month = Integer.parseInt("0"+month);
} if (day<10){
day = Integer.parseInt("0"+day);
}
String date = year + "-" + month + "-" + day;
btnDateOfBirth.setText(date);
}
};
}