8

How to get current time format of the android device? i.e. If the android device's current time format is changed from 12hr to 24 hr format and vice versa, then how to check that setting programatically?

I want to change the time shown in my app widget to be of same format as the device's and whenever the time format is changed then my widget should also show that time format. Any pointers will be helpful.

thanks.

Gaurav Navgire
  • 780
  • 5
  • 17
  • 29

4 Answers4

19

You can use TIME_12_24.

int time_format = 0;
try {
    time_format = Settings.System.getInt(getContentResolver(), Settings.System.TIME_12_24);
} catch (SettingNotFoundException e) {
    e.printStackTrace();
}
Log.i(TAG,"Time format: "+time_format);

time_format will have 12 or 24 based on the settings.

Make sure you have added this permission to manifiest file.

<user-permission android:name="android.permission.WRITE_SETTINGS" />

EDIT :
You can also use DateFormat.is24HourFormat(). It doesn't require an extra permission.

Matt Becker
  • 2,338
  • 1
  • 28
  • 36
Karthik
  • 3,509
  • 1
  • 20
  • 26
3

The simple way would be to use to use android native android.text.format.DateFormat class's is24HourFormat(this) static method as below.

if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) {
          // 24 hour format
}
else
{
    // 12 hour format
}
Shivanand Darur
  • 3,158
  • 1
  • 26
  • 32
1

check, http://developer.android.com/reference/android/text/format/DateFormat.html

DateFormat::getDateFormat()

Date date = new Date(location.getTime());
dateFormat.format(date);
Vamsi
  • 5,853
  • 6
  • 29
  • 36