您的位置:首页 > 移动开发 > Android开发

获取Android系统时间是24小时制还是12小时制

2013-09-09 15:18 399 查看
这个是在网上能搜到的方法,用来判断当前系统是12小时制或者24小时制,当时做项目用的就是这个判断。

ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);

if(strTimeFormat.equals("24"))

{
Log.i("activity","24");
}
以上来自:/article/7055700.html,感谢他的分享,解决了我当时的问题。

最近没事看了看DeskClock的源码,里边也有一个判断是否是24小时制的方法,API提供的,写的更加完善,考虑的更加周全。

/**
* @return true if clock is set to 24-hour mode
*/
public static boolean get24HourMode(final Context context) {
return android.text.format.DateFormat.is24HourFormat(context);
}


进入源码

/**
* Returns true if user preference is set to 24-hour format.
* @param context the context to use for the content resolver
* @return true if 24 hour time format is selected, false otherwise.
*/
public static boolean is24HourFormat(Context context) {
String value = Settings.System.getString(context.getContentResolver(),
Settings.System.TIME_12_24);

if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;

synchronized (sLocaleLock) {
if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
return sIs24Hour;
}
}

java.text.DateFormat natural =
java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);

if (natural instanceof SimpleDateFormat) {
SimpleDateFormat sdf = (SimpleDateFormat) natural;
String pattern = sdf.toPattern();

if (pattern.indexOf('H') >= 0) {
value = "24";
} else {
value = "12";
}
} else {
value = "12";
}

synchronized (sLocaleLock) {
sIs24HourLocale = locale;
sIs24Hour = value.equals("24");
}

return sIs24Hour;
}

return value.equals("24");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: