binding.datePicker.setOnClickListener {
val mCurrentDate: Calendar = Calendar.getInstance()
val mYear: Int = mCurrentDate.get(Calendar.YEAR)
val mMonth: Int = mCurrentDate.get(Calendar.MONTH)
val mDay: Int = mCurrentDate.get(Calendar.DAY_OF_MONTH)
val mDatePicker = DatePickerDialog(
requireActivity(), R.style.DialogTheme,
{ datepicker, selectedyear, selectedmonth, selectedday ->
val selectedmonth = selectedmonth + 1
val mFormat = DecimalFormat("00")
mFormat.roundingMode = RoundingMode.DOWN
val date: String =
mFormat.format((selectedyear)) + "-" + mFormat.format((selectedmonth)
) + "-" + mFormat.format((selectedday))
binding.edtDob.setText(date)
}, mYear - 18, mMonth, mDay - 1
)
// set maximum date to be selected as today
// age more then 18+
mCurrentDate.add(Calendar.YEAR, -18);
mCurrentDate.add(Calendar.DAY_OF_MONTH, -1)
mDatePicker.datePicker.maxDate = mCurrentDate.timeInMillis
mDatePicker.show()
}
www.un.org/spanish/ecosoc 但是,你只需要这一部分。
val mFormat = DecimalFormat("00")
mFormat.roundingMode = RoundingMode.DOWN
val date: String = mFormat.format((selectedyear)) + "-" + mFormat.format((selectedmonth)) + "-" + mFormat.format((selectedday))
binding.edtDob.setText(date)