Search code examples
javaandroidmobilematerial-components-androidandroid-datepicker

MaterialDatePicker.getHeaderText() returns 00 on month android


here is my code example

i have a material date picker which i want to use to get a date my problem is when i try to format it to 'yyyy-mm-dd' it always returns "00" in months for example if a string "Mar 5,2021" the result will be "2021-00-05" any help will be appreciated thank you


Solution

  • The addOnPositiveButtonClickListener returns the selection as Long value. You don't need to get the HeaderText.

    Use for example:

    datePicker.addOnPositiveButtonClickListener {
        val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
        utc.timeInMillis = it
        val format = SimpleDateFormat("yyyy-MM-dd")
        val formatted: String = format.format(utc.time)        
    }