Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:Changing time format as per the Device's Current time format

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.

like image 756
Gaurav Navgire Avatar asked Jan 12 '12 11:01

Gaurav Navgire


People also ask

How do I change my android from 12 hours to 24 hours?

You can return to 12-hour time by tapping Use 24-Hour Format again. Tap Select Date Format. Tap one of the three format options to change the date format. If you don't want to change the date format, tap Cancel.

How do I check whether a phone time format is set to 24 hours?

You can find this option in Settings > General > Language and Region > Advanced.


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

like image 139
Karthik Avatar answered Nov 03 '22 00:11

Karthik


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

DateFormat::getDateFormat()

Date date = new Date(location.getTime());
dateFormat.format(date);
like image 37
Vamsi Avatar answered Nov 03 '22 01:11

Vamsi