I am a French Android developer, so using Locale.getDefault()leads to mine DateFormatusing 24-hour mode. But when I manually set my device to 12-hour mode through the settings menu, it DateFormatcontinues to work in a 24-hour format.
On the contrary, they TimePickerare installed in accordance with my settings 12/24 hours.
Is there a way to DateFormatmake myself behave the same way TimePicker?
EDIT:
Here is my announcement DateFormat:
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
And this is where I set mine TimePickerto 12 or 24 hour mode.
tp.setIs24HourView(android.text.format.DateFormat.is24HourFormat((Context) this));
My decision:
According to @Meno Hochschild below, here is how I solved this complex problem:
boolean is24hour = android.text.format.DateFormat.is24HourFormat((Context) this);
tp.setIs24HourView(is24hour);
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (timeFormat instanceof SimpleDateFormat) {
String pattern = ((SimpleDateFormat) timeFormat).toPattern();
if (is24hour) {
timeFormat = new SimpleDateFormat(pattern.replace("h", "H").replace(" a",""), Locale.getDefault());
}
else {
timeFormat = new SimpleDateFormat(pattern.replace("H", "h"), Locale.getDefault());
}
}
timeFormat , 24- 12- . TimePicker .