Im trying to have my datepicker display the predefined date that I have set when I click on it. I have tried using a delimiter to separate the date and setting them based on the year, month and day but it still does not work. it keeps on displaying the current date instead of the date i set
val date = '03-22-2022'
val startCalendar = Calendar.getInstance()
val startPicker = DatePickerDialog.OnDateSetListener{ view, year, month, dayOfMonth ->
startCalendar.set(Calendar.YEAR,year)
startCalendar.set(Calendar.MONTH,month)
startCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)
}
editstartdatepicker.setOnClickListener{
val dialog = DatePickerDialog(this,
startPicker,
startCalendar.get(Calendar.YEAR),
startCalendar.get(Calendar.MONTH),
startCalendar.get(Calendar.DAY_OF_MONTH))
dialog.show()
}
CodePudding user response:
You should initialize startCalender as it's pointing to current date use this
val date = "03-22-2022"
val startCalendar = Calendar.getInstance()
val sdf = SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH)
startCalendar.time = sdf.parse(date)
CodePudding user response:
may it helps
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.updateDate(2016, 5, 22);