Dayana
Dayana

Reputation: 23

Set date and get Date from Material Date Picker in Kotlin Android

I'm learning kotlin and at the moment I don't know much, I want to change a datepicker that I have for one of type Material, the problem is that I don't know how to pass the data to this new date picker.

This is the one I have at the moment:

fecha = view.findViewById(R.id.fecha)
fecha?.setOnClickListener {
  fecha!!.error = null
  val dateSetListener = DatePickerDialog(requireContext(), { _, year, monthOfYear, dayOfMonth ->
   cal.set(Calendar.YEAR, year)
   mYear = year
   cal.set(Calendar.MONTH, monthOfYear)
   mMonth = monthOfYear
   cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
   mDay = dayOfMonth
   updateDateInView()
}, mYear, mMonth, mDay)
  dateSetListener.datePicker.maxDate = System.currentTimeMillis()
  dateSetListener.show()
}

fun updateDateInView() {
   val myFormat = "dd/MM/yyyy"
   val sdf = SimpleDateFormat(myFormat)
   fecha?.setText(sdf.format(cal.time))
}

I want to make it like this but I don't know how to pass and save the values, could someone help me?

val datePicker = MaterialDatePicker.Builder.datePicker().build()

Upvotes: 0

Views: 3870

Answers (1)

Praveen
Praveen

Reputation: 3486

MaterialDatePicker accepts CalendarConstraints to open the date picker on a certain month. CalendarConstraints accepts timeInMilliseconds to open calendar on a particular month. MaterialDatePicker has an addOnPositiveButtonClickListener method whose lambda returns the time in milliseconds after the user makes a selection.

You can create MaterialDatePicker like this

val myFormat = "dd/MM/yyyy"
val formattedDate = "01/01/2000"

val sdf = SimpleDateFormat(myFormat)
val date = sdf.parse(formattedDate)
val timeInMillis = date.time

val constraintBuilder = CalendarConstraints.Builder().setOpenAt(
    timeInMillis //pass time in milli seconds 
).build()

val picker = MaterialDatePicker.Builder.datePicker()
    .setTitleText("Select Date")
    .setCalendarConstraints(constraintBuilder)
    .build()

picker.addOnPositiveButtonClickListener {
    val date = Date(it)
    val formattedDate = sdf.format(date) // date selected by the user
}

// show picker using this
picker.show(requireActivity().supportFragmentManager, "materialDatePicker")

Instead of using SimpleDateFormatter, you should use LocalDateTime API provided in Java8

Using LocalDateTime API, you can do the same like this

val dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.getDefault())
val formattedDate = "01/01/2000"
val timeInMillis = LocalDate.parse(formattedDate, dateFormatter)
                    .atStartOfDay(ZoneId.systemDefault())
                    .toInstant()
                    .toEpochMilli()
                

You can pass this timeInMillis to setOpenAt() method of CalendarConstraintSet.

To get the date after the user makes a selection

val timeInMillis = dateFormatter.format(
                    // it is the milliseconds received inside lambda of addOnPositiveButtonClickListener
                    Instant.ofEpochMilli(it) 
                        .atZone(ZoneId.systemDefault()).toLocalDate()
                )

Upvotes: 2

Related Questions