I want to format a time display to conform to both the current Locale, and the device user's 24-hour clock preference. Here is what I'm currently doing, but I'm wondering if there's a better way.
val time = ZonedDateTime.now()
val formatter = when (DateFormat.is24HourFormat(context)) {
true -> DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault())
false -> DateTimeFormatter.ofPattern("h:mm a", Locale.getDefault())
}
text = time.format(formatter)
Is there any way to get the user's 24 hour preference aside from using DateFormat (since that class is obsolete)?
Is there a better way to create the formatter? DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) doesn't respect the user's 24 hour preference.