安卓:更改时间格式为每个设备的当前时间格式 [英] Android:Changing time format as per the Device's Current time format

查看:237
本文介绍了安卓:更改时间格式为每个设备的当前时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得Android设备的当前时间格式?即如果Android设备的当前时间格式由12小时改为24小时格式,反之亦然,那么如何检查设置编程?

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.

感谢。

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/provider/Settings.System.html#TIME_12_24">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 12 24 的基础上的设置。

time_format will have 12 or 24 based on the settings.

请确保您已经添加此权限manifiest文件。

Make sure you have added this permission to manifiest file.

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

编辑:
您也可以使用<一个href="http://developer.android.com/reference/android/text/format/DateFormat.html#is24HourFormat%28android.content.Context%29">DateFormat.is24HourFormat().它不需要额外的许可。

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

这篇关于安卓:更改时间格式为每个设备的当前时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆