Android: change the time format to match the current time format of the device

How to get the current time format for an Android device? those. If the format of the current time of the Android device is changed from 12 to 24 hours and vice versa, then how to check this parameter programmatically?

I want to change the time shown in the widgets of my application in the same format as the device, and whenever the time format changes, my widget should also show this time format. Any pointers would be helpful.

thanks.

+6
source share
4 answers

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 settings.

Make sure you add this permission to the manifest file.

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

EDIT:
You can also use DateFormat.is24HourFormat () . This does not require additional permission.

+16
source

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

DateFormat::getDateFormat()

 Date date = new Date(location.getTime()); dateFormat.format(date); 
+1
source

An easy way would be to use the static method android.text.format.DateFormat class is24HourFormat(this) , based on android, as shown below.

 if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) { // 24 hour format } else { // 12 hour format } 
+1
source

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


All Articles