Android:根据设备的当前时间格式更改时间格式

Gam*_*oid 8 time android android-widget

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

我想将我的app小部件中显示的时间更改为与设备格式相同的格式,并且每当更改时间格式时,我的小部件也应该显示该时间格式.任何指针都会有所帮助.

谢谢.

Kar*_*hik 18

您可以使用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);
Run Code Online (Sandbox Code Playgroud)

time_format将根据设置有1224.

确保您已将此权限添加到最明显的文件中.

<user-permission android:name="android.permission.WRITE_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)

编辑:
您还可以使用DateFormat.is24HourFormat().它不需要额外的许可.