12/24 hour conflict mode

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); // tp is the TimePicker
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 .

+4
4

SimpleDateFormat, 12/24- , 12- "h" (1-12), 24- "H" (0-23). "k" "K" .

, !

DateFormat.getDateTimeInstance(), ( Locale.getDefault() - , , Android-Java Locale.setDefault()).

, Android, , TIME_12_24, , . DateFormat.is24HourFormat() ( , Android DateFormat)., :

boolean twentyFourHourStyle = 
  android.text.format.DateFormat.is24HourFormat((Context) this);
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (df instanceof SimpleDateFormat) {
  String pattern = ((SimpleDateFormat) df).toPattern();
  if (twentyFourHourStyle) {
    df = new SimpleDateFormat(pattern.replace("h", "H"), Locale.getDefault());
  } else {
    df = new SimpleDateFormat(pattern.replace("H", "h"), Locale.getDefault());
  }
} else {
  // nothing to do or change
}

, , k K h H ( , ).

+4

. :

Locale.US 12 , 12 .

, 24- , Locale 24 .

boolean twentyFourHourStyle = android.text.format.DateFormat.is24HourFormat(context);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
if (twentyFourHourStyle) {
    timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT,Locale.FRANCE);
}

3 ;)

+1

, DateFormat.

android.text.format.DateFormat.getTimeFormat();

Time 12/24 .

: java.text.DateFormat. .

. = (); String time = android.text.format.DateFormat.getTimeFormat(context).format(date);

0

, , , DateFormat.getTimeInstance(DateFormat.SHORT) . API 26 (Android O) AVD, API 16 API 19 (Android 4). , , - . android.text.format.DateFormat.getTimeFormat() android.text.format.DateFormat.is24HourFormat(), . AVD, .

0

Source: https://habr.com/ru/post/1537520/


All Articles