Jetpack Compose DatePicker — Complete Guide
In this tutorial, you'll learn about Jetpack Compose DatePicker. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your Material 3 DatePicker doesn't respond to taps, selecting a date returns the wrong value, or the dialog dismisses before selection.
Wrong Approach ❌
@Composable
fun BadDatePicker() {
val datePickerState = rememberDatePickerState()
// DatePicker without OK/Cancel buttons
DatePicker(state = datePickerState)
}
// Using string instead of Long for selected date
val selectedDate = datePickerState.selectedDateMillis?.toString() // BUG: milliseconds as string
Output: Missing confirm/cancel buttons. Date string shows milliseconds.
Right Approach ✅
@Composable
fun GoodDatePicker() {
var showDialog by remember { mutableStateOf(false) }
var selectedDateText by remember { mutableStateOf("") }
OutlinedButton(onClick = { showDialog = true }) {
Text(if (selectedDateText.isEmpty()) "Pick a date" else selectedDateText)
}
if (showDialog) {
val datePickerState = rememberDatePickerState(
initialSelectedDateMillis = System.currentTimeMillis(),
selectableDates = object : SelectableDates {
override fun isSelectableDate(utcTimeMillis: Long): Boolean {
return utcTimeMillis <= System.currentTimeMillis() // Past dates only
}
}
)
DatePickerDialog(
onDismissRequest = { showDialog = false },
confirmButton = {
TextButton(onClick = {
datePickerState.selectedDateMillis?.let { millis ->
val formatter = SimpleDateFormat("MM/dd/yyyy", Locale.getDefault())
selectedDateText = formatter.format(Date(millis))
}
showDialog = false
}) { Text("OK") }
},
dismissButton = {
TextButton(onClick = { showDialog = false }) { Text("Cancel") }
}
) {
DatePicker(state = datePickerState)
}
}
}
// Date range picker
val rangePickerState = rememberDateRangePickerState()
DatePickerDialog(onDismissRequest = {}, confirmButton = {}, dismissButton = {}) {
DateRangePicker(state = rangePickerState)
}
Output: Working date picker dialog with proper date formatting.
Prevention
- Always wrap
DatePickerinDatePickerDialogfor OK/Cancel buttons. - Use
SimpleDateFormatorjava.timeformatting for user display. - Use
SelectableDatesto restrict selectable dates. - Convert
selectedDateMillisfrom UTC millis to readable format.
Common Mistakes with compose date picker
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro