获取Android系统时间是24小时制还是12小时制
16lz
2021-01-25
这个是在网上能搜到的方法,用来判断当前系统是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"); }
以上来自: http://www.cnblogs.com/liwqiang/archive/2010/08/08/1795172.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"); }
更多相关文章
- 复习android 的大喇叭~~~
- Android(安卓)开发之 Drawable系列
- Android(安卓)5.0源码编译问题
- MTK android配置LCD背光和LED,调试方法
- Android(安卓)kernel x86 编译方法
- Android(安卓)Service之bindService()
- android 扫描文件栈溢出问题,自己写的扫描文件方法
- Android经典游戏源码汇总
- 【Android习惯】文件、方法、变量命名规范参考(编辑中)