Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

predefine date to display on datepicker in android

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

        }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading