Javi Moya
Javi Moya

Reputation: 193

timepicker: when to choose 24 or am/pm modes

I have a few timepickers in my app.

In my country (Spain) we are used to displaying time in 24 hours mode... but in other countries are used to am/pm.

I know how to set a Timepicker to 24 or am/pm mode... But what is the best approach to show am/pm or 24 depending of the device locale or country? How can I know to select one or another mode?

Thank you very much

(Sorry for my poor english)

Upvotes: 10

Views: 3585

Answers (2)

MrJre
MrJre

Reputation: 7161

There are several ways to do this, of which the first is to just use the device settings and thus let the user make this decision, but be aware that this can return a null value in some cases and is therefore not as reliable;

String clockType = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24);

A better solution is given by wonder.mice, which is using:

boolean is24HourFormat = DateFormat.is24HourFormat();

see also the reference page: http://developer.android.com/reference/android/text/format/DateFormat.html#is24HourFormat(android.content.Context)

Upvotes: 9

wonder.mice
wonder.mice

Reputation: 7583

Use DateFormat from android.text.format.DateFormat: http://developer.android.com/reference/android/text/format/DateFormat.html

As can be seen from sources (frameworks/base/core/java/android/text/format/DateFormat.java) function DateFormat.is24HourFormat() does much more than just asking for TIME_12_24 from Settings.System.

As I understand DateFormat.is24HourFormat() is a more reliable way to get users time format. For example, because Settings.System.getString() can return null for TIME_12_24 (as seen from DateFormat.java).

Upvotes: 13

Related Questions