androidandroid-datepickermaterialdatepicker

Selected date Month looks like a code not a string


Everyone

I'm using MaterialDatePicker to show the date and it was working fine and get the date like I want Aug 5,2021 and suddenly out of nowhere I get now Month like this M08 and that makes the date like this M08 5,2021

enter image description here

and below is the gradle dependency:

implementation "com.google.android.material:material:1.3.0"

Can anyone help me, please?


Solution

  • you can try this code

        private fun showDatePicker() {
            val selectedDateInMillis = currentSelectedDate ?: System.currentTimeMillis()
            
            MaterialDatePicker.Builder.datePicker().setSelection(selectedDateInMillis).build().apply {
                addOnPositiveButtonClickListener { dateInMillis -> onDateSelected(dateInMillis) }
            }.show(supportFragmentManager, MaterialDatePicker::class.java.canonicalName)
        }
    
        // result onDateSelected    
        private fun onDateSelected(dateTimeStampInMillis: Long) {
            currentSelectedDate = dateTimeStampInMillis
            val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentSelectedDate), ZoneId.systemDefault())
            val dateAsFormattedText: String = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            // view for result
            findViewById<TextView>(R.id.output).text = dateAsFormattedText
       }